View Code of Problem 17

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

int a[200][200];
int maxNum(int a,int b,int c,int d){
	if(a>=b&&a>=c&&a>=d){
		return a;
	}
	if(b>=a&&b>=c&&b>=d){
		return b;
	}
	if(c>=a&&c>=b&&c>=d){
		return c;
	}
	if(d>=a&&d>=b&&d>=c){
		return d;
	}
}
int maxstep(int a[][200],int i,int j,int n,int m){
	if(i<0||i>=n||j<0||j>=m){
		return 0;
	}else{
		int shang=0,xia=0,zuo=0,you=0,max=0;
		if(a[i-1][j]>a[i][j]){
			shang=maxstep(a,i-1,j,n,m);
		}
		if(a[i+1][j]>a[i][j]){
			xia=maxstep(a,i+1,j,n,m);
		}
		if(a[i][j-1]>a[i][j]){
			zuo=maxstep(a,i,j-1,n,m);
		}
		if(a[i][j+1]>a[i][j]){
			you=maxstep(a,i,j+1,n,m);
		}
		if(shang==0&&xia==0&&zuo==0&&you==0){
			return 1;
		}
		max=maxNum(shang,xia,zuo,you);
		return max+1;
	}
}
int main(){
	int n,m;
	while(scanf("%d %d",&n,&m)!=EOF){
		for(int i=0;i<n;i++){
		for(int j=0;j<m;j++){
			scanf("%d",&a[i][j]);
		}
	}
	int max=0;
	for(int i=0;i<n;i++){
		for(int j=0;j<m;j++){
			int step=maxstep(a,i,j,n,m); 
			if(step>max){
				max=step;
			}
		}
	}
	printf("%d\n",max-1);
	}
	
}
				

Double click to view unformatted code.


Back to problem 17