View Code of Problem 17

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

int search(int a[20][20],int i,int j){
	int left=0, right = 0,up = 0, down = 0, max = 0;
	if(a[i][j]>a[i-1][j] && a[i][j]>a[i][j-1] && a[i][j]>a[i+1][j] && a[i][j]>a[i][j+1]){
		return 0;
	}else{
		if(a[i][j]<a[i-1][j]){
		left = search(a,i-1,j) + 1;
		}
		if(a[i][j]<a[i][j+1]){
			up = search(a,i,j+1) + 1;
		}
		if(a[i][j]<a[i+1][j]){
			right = search(a,i+1,j) + 1;
		}
		if(a[i][j]<a[i][j-1]){
			down = search(a,i,j-1) + 1;
		}
		int max1 = left>right?left:right;
		int max2 = up>down?up:down;
		max = max1>max2?max1:max2;
		return max;
	}
}


int main(){
	int m,n;
	
	while((scanf("%d %d",&n,&m)!=EOF)){
		int a[20][20]={0};
		for(int i=1; i<=n; i++){
			for(int j=1; j<=m; j++){
				scanf("%d",&a[i][j]);
			}
		}
		int max = 0;
		int len = 0;
		for(int i=1; i<=n; i++){
			for(int j=1; j<=m; j++){
				len = search(a,i,j);
				if(len>max)
					max = len;
			}
		
		}
		printf("%d\n",max);
	}

	return 0;
}

Double click to view unformatted code.


Back to problem 17