#include <stdio.h>
#include <string.h>
#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;
} 南阳理工第十题
最新推荐文章于 2024-11-16 09:24:36 发布
本文介绍了一个使用深度优先搜索算法解决的问题:在一个给定的二维数组中寻找从任意位置出发的最大路径长度,路径只能沿上下左右四个方向移动,并且只能从较高的高度移动到较低的高度。
2429

被折叠的 条评论
为什么被折叠?



