滑雪
Time Limit: 1 Sec Memory Limit: 256 MB
题目连接
http://www.tyvj.cn/p/1004
Description
trs喜欢滑雪。他来到了一个滑雪场,这个滑雪场是一个矩形,为了简便,我们用r行c列的矩阵来表示每块地形。为了得到更快的速度,滑行的路线必须向下倾斜。
例如样例中的那个矩形,可以从某个点滑向上下左右四个相邻的点之一。例如24-17-16-1,其实25-24-23…3-2-1更长,事实上这是最长的一条。
Input
输入文件
第1行: 两个数字r,c(1<=r,c<=100),表示矩阵的行列。
第2..r+1行:每行c个数,表示这个矩阵。
Output
输出文件
仅一行: 输出1个整数,表示可以滑行的最大长度。
Sample Input
5 5
1 2 3 4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9
Sample Output
25
HINT
# include <iostream>
# include <cstring>
using namespace std;
const int maxn = 105;
int dp[maxn][maxn];
int sum[maxn][maxn];
int dir[][2] = {0, 1, 0 , -1, 1, 0, -1, 0};
int r, c, maxx;
void dfs(int x, int y)
{
if(dp[x][y])
{
return ;
}
dp[x][y] = 1;//如果我上下左右都不行那么我当前的最优解就是1
for(int i = 0; i < 4; i++)
{
int tx = x + dir[i][0];
int ty = y + dir[i][1];
if((tx < 0 || tx >= r || ty < 0 || ty >= c) || sum[x][y] <= sum[tx][ty])
{
continue;
}
dfs(tx, ty);
dp[x][y] = max(dp[x][y], dp[tx][ty] + 1);//更新值,我的上一步加+1==我的动态dp[x][y]和当前dp[x][y]比较找最大的
}
return ;
}
int main(int argc, char *argv[])
{
while(cin >> r >> c)
{
memset(dp, 0, sizeof(dp));
for(int i = 0; i < r; i++)
{
for(int j = 0; j < c; j++)
{
cin >> sum[i][j];
}
}
maxx = 0;
for(int i = 0; i < r; i++)
{
for(int j = 0; j < c; j++)
{
dfs(i, j);
maxx = max(maxx, dp[i][j]);
}
}
cout << maxx << endl;
}
return 0;
}