您的位置:首页 > 其它

昨晚的笔试题Skiing,动归+深搜

2016-03-23 10:27 267 查看
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#define MAX(a,b) ((a)>(b)?(a):(b))
using namespace std;
int height[120][120] = { 0 };//保存输入高度值
int length[120][120] = { 0 };//计算后的长度值
int R, C;
int dfs(int r, int c)
{
if (height[r][c] == -1)
{
return 0;
}
if(length[r][c] > 0)
return length[r][c];
int p = 0;
int q = 0;
int m = 0;
int n = 0;
if (height[r - 1][c] != -1 && height[r - 1][c] < height[r][c])
{
p = dfs(r - 1, c);
}
if (height[r + 1][c] != -1 && height[r + 1][c] < height[r][c])
{
q = dfs(r + 1, c);
}
if (height[r ][c - 1] != -1 && height[r][c - 1] < height[r][c])
{
m = dfs(r , c - 1);
}
if (height[r][c + 1] != -1 && height[r][c + 1] < height[r][c])
{
n = dfs(r , c + 1);
}
length[r][c]=MAX(MAX(p,q),MAX(m,n))+1;

return length[r][c];
}
int ans()
{
int i, j;
for (i = 1; i <= R; ++i)
for (j = 1; j <= C; ++j)
length[i][j] = 0;
int maxLength = 0;
for (i = 1; i <= R; ++i)
for (j = 1; j <= C; ++j)
{
int num = dfs(i, j);
if(maxLength < num)
maxLength=num;
}
return maxLength;
}
int main()
{
int T;
int i, j;
memset(height,-1,sizeof(height));

//freopen("in.txt", "r", stdin);
cin >> T;
while (T--)
{
cin >> R >> C;
for (i = 1; i <= R; ++i)
for (j = 1; j <= C; ++j)
cin >> height[i][j];//最外层为-1

cout << ans() << endl;

}
//fclose(stdin);
//system("pause");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: