南阳理工oj 10 skiing 动态规划

http://acm.nyist.net/JudgeOnline/problem.php?pid=10

简单的动态规划题,在深搜时需要记录每一个的状态,然后直接就调用了,省了很多时间。

#include 
#include 

#define MAX 101

int height[MAX][MAX];
int tmp[MAX][MAX];

int r,c;

int dir[4][2] = {-1, 0, 1, 0, 0, 1, 0, -1};

bool isvalueable(int x, int y)
{
	if(x > 0 && x <= r && y > 0 && y <= c)return true;
	return false;
}

int dfs(int x, int y)
{
	if(tmp[x][y] > 0)return tmp[x][y];
	int max = 0,i;
	for(i = 0; i <  4; i ++){
		int tx = x + dir[i][0];
		int ty = y + dir[i][1];
		if(isvalueable(tx,ty)){
			if(height[x][y] > height[tx][ty]){
				int m = dfs(tx, ty)+1;
				if(max < m)max = m;
			}
		}
	}
	return max;
}

int main()
{
	int t;
	int i, j, res;
	scanf("%d", &t);
	while(t--){
		res = 0;
		scanf("%d%d", &r, &c);
		for(i = 1;i <= r; i ++){
			for(j = 1; j <= c; j ++){
				scanf("%d", &height[i][j]);
				tmp[i][j] = 0;
			}
		}
		for(i = 1; i <= r; i ++){
			for(j = 1; j <= c; j ++){
				tmp[i][j] = dfs(i, j);
				if(tmp[i][j] > res)res = tmp[i][j];
			}
		}
		printf("%d\n",res+1);
	}
	return 0;
}


你可能感兴趣的:(动态规划)