View Code of Problem 17

#include<stdlib.h>
#include<stdio.h>


int n,m;
int stair[1000][1000]={0};
int search(int i,int j){
	if(stair[i][j]>stair[i+1][j] && stair[i][j]>stair[i-1][j] && stair[i][j]>stair[i][j+1] && stair[i][j]>stair[i][j-1])
		return 0;
	
	int l=0,u=0,r=0,d=0;
	if(stair[i][j]<stair[i][j-1]){
		l = 1 + search(i,j-1); 
	}

	if(stair[i][j]<stair[i-1][j]){
		u = 1 + search(i-1,j); 
	}

	if(stair[i][j]<stair[i][j+1]){
		r = 1 + search(i,j+1); 
	}

	if(stair[i][j]<stair[i+1][j]){
		d = 1 + search(i+1,j); 
	}

	int max1,max2;
	if (l>r)
		max1 = l;
	else 
		max1 = r;

	if(u>d)
		max2 = u;
	else
		max2 = d;

	int max;
	if(max1 > max2)
		max = max1;
	else
		max = max2;
	return max;

}


int main(){
	while((scanf("%d %d",&n,&m)!=EOF)){
		for(int i=0; i<=1000; i++)
			for(int z=0; z<=1000; z++)
				stair[i][z]=0;

		for(int i=1; i<=n; i++){
			for(int z=1; z<=m; z++)
				scanf("%d",&stair[i][z]);
		}
		int max=0;
		for(int i=1; i<=n; i++){
			for(int z=1; z<=m; z++){
				int result = search(i,z);
				if(result > max)
					max = result;
			}
		}
		printf("%d\n",max);
	}
	return 0;
}

Double click to view unformatted code.


Back to problem 17