View Code of Problem 17

#include <stdio.h>

int climb(int x[100][100],int i,int j){
    int w=0,a=0,s=0,d=0;
    if(x[i][j]>x[i-1][j]&&x[i][j]>x[i+1][j]&&x[i][j]>x[i][j-1]&&x[i][j]>x[i][j+1]){
        return 0;
    }else{
        if(x[i][j]<x[i-1][j])w=climb(x,i-1,j)+1;
        if(x[i][j]<x[i+1][j])s=climb(x,i+1,j)+1;
        if(x[i][j]<x[i][j-1])a=climb(x,i,j-1)+1;
        if(x[i][j]<x[i][j+1])d=climb(x,i,j+1)+1;
    }
    return (w>s?w:s)>(a>d?a:d)?(w>s?w:s):(a>d?a:d);
}

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

Double click to view unformatted code.


Back to problem 17