View Code of Problem 17

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

Double click to view unformatted code.


Back to problem 17