zoukankan      html  css  js  c++  java
  • E

    Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

    Description

    Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激。可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你。Michael想知道载一个 区域中最长底滑坡。区域由一个二维数组给出。数组的每个数字代表点的高度。下面是一个例子
     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

    一个人可以从某个点滑向上下左右相邻四个点之一,当且仅当高度减小。在上面的例子中,一条可滑行的滑坡为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 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


     1 #include<cstdio>
     2 #include<string.h>
     3 using namespace std;
     4 int s[200][200],dp[200][200];
     5 int R,C;
     6 int max(int a,int b,int c,int d)
     7 {
     8     a=a>b?a:b;
     9     a=a>c?a:c;
    10     a=a>d?a:d;
    11     return a;
    12 }
    13 int  f(int x,int y)
    14 {
    15     if(dp[x][y]>0) return dp[x][y];
    16  int a=0,b=0,c=0,d=0;
    17     if(x-1>=0&&x-1<R&&s[x][y]>s[x-1][y])
    18         a=f(x-1,y);
    19     if(x+1>=0&&x+1<R&&s[x][y]>s[x+1][y])
    20         b=f(x+1,y);                      //判断一个点的四个方向,能不能走
    21     if(y-1>=0&&y-1<C&&s[x][y]>s[x][y-1])
    22         c=f(x,y-1);
    23     if(y+1>=0&&y+1<C&&s[x][y]>s[x][y+1])
    24         d=f(x,y+1);
    25     dp[x][y]=max(a,b,c,d)+1;//取最大值
    26     return dp[x][y];//返回节点的值
    27 }
    28 int main()
    29 {
    30     while(scanf("%d%d",&R,&C)!=EOF)
    31     {
    32         memset(dp,0,sizeof(dp));//清零
    33         for(int i=0;i<R;i++)
    34             for(int j=0;j<C;j++)
    35                scanf("%d",&s[i][j]);
    36         for(int i=0;i<R;i++)
    37             for(int j=0;j<C;j++)
    38                dp[i][j]=f(i,j);//计算每个节点
    39         int m=0;
    40         for(int i=0;i<R;i++)
    41             for(int j=0;j<C;j++)
    42                if(dp[i][j]>m)
    43                     m=dp[i][j];//求出最长路径
    44         printf("%d
    ",m);
    45     }
    46     return 0;
    47 }
  • 相关阅读:
    HDU6256 Master of Phi (狄利克雷卷积、欧拉函数)
    POJ2689 Prime Distance
    演练5-4:Contoso大学校园管理系统4
    Week7(10月24日)
    演练5-3:Contoso大学校园管理系统3
    Week7(10月21日)
    演练5-2:Contoso大学校园管理2
    Week6(10月17日):周末别忘记运动
    演练5-1:Contoso大学校园管理1
    Week6(10月14日)
  • 原文地址:https://www.cnblogs.com/angledamon/p/3891939.html
Copyright © 2011-2022 走看看