hrbust 1179 下山【邻接阵,dfs】

题目连接;http://acm.hrbust.edu.cn/index.php?m=ProblemSet&a=showProblem&problem_id=1179

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

int a[101][101];
int visited[101][101];
int n, m;

int dfs(int i, int j)
{
	if( visited[i][j] ) return visited[i][j];
	int sum = 0;

	if(i - 1 >= 0 && a[i][j] > a[i-1][j] && sum < dfs(i-1, j)) {
		sum = dfs(i-1, j);
	}

	if(i + 1 < n &&  a[i][j] > a[i+1][j] && sum < dfs(i+1, j)) {
		sum = dfs(i+1, j);
	}

	if(j - 1 >= 0 && a[i][j] > a[i][j-1] && sum < dfs(i, j-1)) {
		sum = dfs(i, j-1);
	}

	if(j + 1 < m && a[i][j] > a[i][j+1] && sum < dfs(i, j+1)) {
		sum = dfs(i, j+1);
	}

	return visited[i][j] = sum + 1;
}

int main()
{
	while(scanf("%d %d", &n, &m) != EOF) {
		int i, j;
		for(i = 0; i < n; i++) {
			for(j = 0; j < m; j++) {
				scanf("%d", *(a + i) + j);
			}
		}
		int ans = 0;

		for(i = 0; i < n; i++) {
			for(j = 0; j < m; j++) {
				memset(visited, 0, sizeof(visited));
				int tmp = dfs(i, j);
				ans = ans > tmp ?  ans : tmp;
			}
		} 
		printf("%d\n", ans);
	}
	return 0;
}

  

posted @ 2012-10-31 23:34  小猴子、  阅读(324)  评论(0编辑  收藏  举报