【POJ 1088】滑雪

来源:互联网 发布:西班牙手机网络 编辑:程序博客网 时间:2024/06/01 22:48


Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激。可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你。Michael想知道载一个区域中最长底滑坡。区域由一个二维数组给出。数组的每个数字代表点的高度。下面是一个例子
 1  2  3  4 516 17 18 19 615 24 25 20 714 23 22 21 813 12 11 10 9

一个人可以从某个点滑向上下左右相邻四个点之一,当且仅当高度减小。在上面的例子中,一条可滑行的滑坡为24-17-16-1。当然25-24-23-...-3-2-1更长。事实上,这是最长的一条。
Input
输入的第一行表示区域的行数R和列数C(1 <= R,C <= 100)。下面是R行,每行有C个整数,代表高度h,0<=h<=10000。
Output
输出最长区域的长度。
Sample Input
5 51 2 3 4 516 17 18 19 615 24 25 20 714 23 22 21 813 12 11 10 9
Sample Output
25













题解:

dp, 每次遍历四种状态, 选择比当前低的地方走

dp[i][j] : 从i, j 位置出发的最长路径

dp[i][j] = max(dp[i][j], dp[newI][newJ] + 1);


#include<iostream>#include<algorithm>#include<cstring>#include<cmath>#include<cstdio>#include<queue>#include<vector>using namespace std;const int MXC = 105;const int MXH = 1e4 + 5;int s[MXC][MXC];int dp[MXC][MXC];int r, c;int fx[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};int DP(int x, int y){    if(dp[x][y] != 0)        return dp[x][y];    int nx, ny;    int mx = 1;    for(int i = 0; i < 4; i++)    {        nx = x + fx[i][0];        ny = y + fx[i][1];        if(s[x][y] > s[nx][ny] && nx >= 0 && nx < r && ny >= 0 && ny < c)        {            mx = max(mx, DP(nx, ny) + 1);        }    }    return dp[x][y] = mx;}int main(){    while(scanf("%d%d", &r, &c) != EOF)    {        memset(dp, 0, sizeof(dp));        for(int i = 0; i < r; i++)        {            for(int j = 0; j < c; j++)            {                scanf("%d", &s[i][j]);            }        }        for(int i = 0; i < r; i++)        {            for(int j = 0; j < c; j++)            {                if(dp[i][j] == 0)                    DP(i, j);            }        }        int ans = 0;        for(int i = 0; i < r; i++)        {            for(int j = 0; j < c; j++)            {                ans = max(ans, dp[i][j]);            }        }        printf("%d\n", ans);    }    return 0;}


原创粉丝点击