zoukankan      html  css  js  c++  java
  • UVa 10285 Longest Run on a Snowboard

    Problem C

    Longest Run on a Snowboard

    Input: standard input

    Output: standard output

    Time Limit: 5 seconds

    Memory Limit: 32 MB

    Michael likes snowboarding. That's not very surprising, since snowboarding is really great. The bad thing is that in order to gain speed, the area must slide downwards. Another disadvantage is that when you've reached the bottom of the hill you have to walk up again or wait for the ski-lift.

    Michael would like to know how long the longest run in an area is. That area is given by a grid of numbers, defining the heights at those points. Look at this example:

     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

    One can slide down from one point to a connected other one if and only if the height decreases. One point is connected to another if it's at left, at right, above or below it. In the sample map, a possible slide would be 24-17-16-1 (start at 24, end at 1). Of course if you would go 25-24-23-...-3-2-1, it would be a much longer run. In fact, it's the longest possible.

    Input

    The first line contains the number of test cases N. Each test case starts with a line containing the name (it's a single string), the number of rows Rand the number of columns C. After that follow R lines with C numbers each, defining the heights. R and C won't be bigger than 100N not bigger than 15 and the heights are always in the range from 0 to 100.

    For each test case, print a line containing the name of the area, a colon, a space and the length of the longest run one can slide down in that area.

    Sample Input
    2
    Feldberg 10 5
    56 14 51 58 88
    26 94 24 39 41
    24 16 8 51 51
    76 72 77 43 10
    38 50 59 84 81
    5 23 37 71 77
    96 10 93 53 82
    94 15 96 69 9
    74 0 62 38 96
    37 54 55 82 38
    Spiral 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

    Feldberg: 7
    Spiral: 25

    (Math Lovers’ Contest, Problem Setter: Stefan Pochmann)

    经典的滑雪问题,初学动态规划的时候曾在POJ上写过一次,当时写的还很艰难,这次一下就过了哈哈

    设dp[i][j]表示从第i行第j列出发可以达到的最长路径,则有:

      dp[i][j]=max{dp[i-1][j],dp[i+1][j],dp[i][j-1],dp[i][j+1]}+1 (前提是从(i,j)点要可以走到相邻的那个点,即只有相邻点比点(i,j)低才可以转移)

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<string>
     4 #include<cstring>
     5 #define INF 0x7fffffff
     6 
     7 using namespace std;
     8 
     9 int r,c;
    10 string name;
    11 int G[105][105],dp[105][105];
    12 
    13 int f(int x,int y)
    14 {
    15     if(dp[x][y]>=0)
    16         return dp[x][y];
    17 
    18     int Max=1;
    19     if(G[x-1][y]<G[x][y]&&f(x-1,y)+1>Max)
    20         Max=dp[x-1][y]+1;
    21     if(G[x+1][y]<G[x][y]&&f(x+1,y)+1>Max)
    22         Max=dp[x+1][y]+1;
    23     if(G[x][y-1]<G[x][y]&&f(x,y-1)+1>Max)
    24         Max=dp[x][y-1]+1;
    25     if(G[x][y+1]<G[x][y]&&f(x,y+1)+1>Max)
    26         Max=dp[x][y+1]+1;
    27 
    28     return dp[x][y]=Max;
    29 }
    30 
    31 int main()
    32 {
    33     int kase;
    34 
    35     scanf("%d",&kase);
    36 
    37     while(kase--)
    38     {
    39         cin>>name;
    40         scanf("%d %d",&r,&c);
    41 
    42         for(int i=0;i<105;i++)
    43             for(int j=0;j<105;j++)
    44                 G[i][j]=INF;
    45         for(int i=1;i<=r;i++)
    46             for(int j=1;j<=c;j++)
    47                 scanf("%d",&G[i][j]);
    48 
    49         memset(dp,-1,sizeof(dp));
    50 
    51         int ans=-1;
    52         for(int i=1;i<=r;i++)
    53             for(int j=1;j<=c;j++)
    54                 if(f(i,j)>ans)
    55                     ans=dp[i][j];
    56 
    57         cout<<name<<':'<<' '<<ans<<endl;
    58     }
    59 
    60     return 0;
    61 }
    [C++]
  • 相关阅读:
    P3387 【模板】缩点 tarjan
    P2831 愤怒的小鸟 状压dp
    交流帖
    P3959 宝藏 模拟退火。。。
    B1060 [ZJOI2007]时态同步 dfs
    P1850 换教室 概率dp
    树链刨分(待修改)
    B3403 [Usaco2009 Open]Cow Line 直线上的牛 deque
    B3402 [Usaco2009 Open]Hide and Seek 捉迷藏 最短路
    B5248 [2018多省省队联测]一双木棋 状压dp
  • 原文地址:https://www.cnblogs.com/lzj-0218/p/3572375.html
Copyright © 2011-2022 走看看