View Code of Problem 17

#include<stdio.h>
int climb(int arr[100][100], int i, int j){
	int left, right, top, bottom;
	left = right = top = bottom = 0;
	if(arr[i][j] > arr[i][j - 1] && arr[i][j] > arr[i][j + 1] && arr[i][j] > arr[i - 1][j] && arr[i][j] > arr[i + 1][j]){
		return 0;
	} else {
		if(arr[i][j] < arr[i][j - 1]){
			top = climb(arr, i, j - 1) + 1;
		}
		if(arr[i][j] < arr[i][j + 1]){
			bottom = climb(arr, i, j + 1) + 1;
		}
		if(arr[i][j] < arr[i - 1][j]){
			left = climb(arr, i - 1, j) + 1;
		}
		if(arr[i][j] < arr[i + 1][j]){
			right = climb(arr, i + 1, j) + 1;
		}
		int xmax = (left > right ? left : right);
		int ymax = (top > bottom ? top : bottom);
		return xmax > ymax ? xmax : ymax;
	}
}
int main(){
	int n, m;
	while(~scanf("%d%d", &n, &m)){
		int arr[100][100] = {0};
		for(int i = 1; i <= n; i ++){
			for(int j = 1; j <= m; j ++){
				scanf("%d", &arr[i][j]);
			}
		}
		int max = 0, count = 0;
		for(int i = 1; i <= n; i ++){
			for(int j = 1; j <= m; j ++){
				count = climb(arr, i, j);
				if(max < count){
					max = count;
				}
			}
		}
		printf("%d\n", max);
	}
	return 0;
}

Double click to view unformatted code.


Back to problem 17