滑雪

来源:互联网 发布:知乎日报 吐槽精选 编辑:程序博客网 时间:2024/04/29 07:42
Description
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


首先想状态:

判断当前点与上下左右的高度是否递增,

递增那么就找最优,但是这样会不知道

从哪点开始,所以我从高度最低的开始,

然后更新他上下左右的最优值,那样就能得到最优值了。

#include <stdio.h>#include <algorithm>const int maxn = 105;int dp[maxn][maxn], high[maxn][maxn], n, m;struct node{    int x, y, h;    friend bool operator < ( node n1, node n2 )    {        return n1.h < n2.h;    }}a[maxn*maxn];int dx[4] = { 1, 0, -1, 0 }, dy[4] = { 0, 1, 0, -1 };inline int Max ( int a, int b ) { return a > b ? a : b; }int check ( int x, int y ) { return x < 1 || x > n || y < 1 || y > m; }int main ( ){    int v, cnt = 0, ans = 0;    scanf ( "%d%d", &n, &m );    for ( int i = 1; i <= n; i ++ )        for ( int j = 1; j <= m; j ++ )        {            scanf ( "%d", &high[i][j] );            a[cnt].x = i, a[cnt].y = j, a[cnt].h = high[i][j];            cnt ++;            dp[i][j] = 1;        }    std :: sort ( a, a+cnt );   //根据高度排序    for ( int i = 0; i < cnt; i ++ )    {        int x = a[i].x, y = a[i].y;        for ( int j = 0; j < 4; j ++ )        {            int nx = x+dx[j], ny = y+dy[j];            if ( high[nx][ny] > high[x][y] )    //更新上下左右的最优值                dp[nx][ny] = Max ( dp[nx][ny], dp[x][y]+1 );            //printf ( "%d %d %d %d %d\n", x, y, nx, ny, dp[nx][ny] );            ans = Max ( ans, Max ( dp[nx][ny], dp[x][y] ) );        }    }    printf ( "%d", ans );    return 0;}


0 0