View Code of Problem 17

#include<stdio.h>
int main(){
	int m,n,i,j;
	while(scanf("%d %d",&m,&n)!=EOF)
	{
	int a[m][n];
	for(i=0;i<m;i++){
		for(j=0;j<n;j++)
		{
			scanf("%d",&a[i][j]);
		}
	}
    int k=0;
    i=j=0;
    while(k<m*n){
    	if(a[i][j+1]-a[i][j]==1)
    	{
    		k++;
    		j=j+1;
		}
		else if(a[i][j-1]-a[i][j]==1)
		{
    		k++;
    		j=j-1;
		}
		else if(a[i+1][j]-a[i][j]==1){
			k++;
			i=i+1;
		}
		else if(a[i-1][j]-a[i][j]==1){
			k++;
			i=i-1;
		}
		else{
			printf("%d\n",k);
			break;
		}
	}
}
}

Double click to view unformatted code.


Back to problem 17