View Code of Problem 17

#include <cstdio>
#include <iostream>

using namespace std;
struct sstep{
	int i;
	int j;
};

int main(){
	int n,m;
	cin>>n>>m;
	sstep step[n*m+1]={0};
	int mmap[n+2][m+2]={0};
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			int high;
			cin>>high;
			step[high].i=i;
			step[high].j=j;
			mmap[i][j]=high; 
		}
	}
	int max=0;
	int count=0;
	sstep ss=step[1];
	for(int i=2;i<=m*n+1;i++){//从台阶1开始走 
		int r=ss.i;
		int c=ss.j;
		int top=mmap[r-1][c];
		int left=mmap[r][c-1];
		int right=mmap[r][c+1];
		int bottom=mmap[r+1][c];
		if(top!=i&&left!=i&&right!=i&&bottom!=i){
		
			ss=step[i]; 
			if(max<count)
				max=count;
			count=0;
		}
		else{
			count++;
			ss=step[i];
		}
	}
	cout<<max<<endl;
	
}

Double click to view unformatted code.


Back to problem 17