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++]
  • 相关阅读:
    Centos8 静态IP设置
    2022年1月6号 LocalDateTime触发列无效问题
    2021年12月14日复盘(Oracle Not In,Limit 1000)
    2022年1月2日复盘 线上CPU飙升
    2021年12月16日复盘 JSQLParser 命中Oracle关键词报错
    2022年1月5号 on update CURRENT_TIMESTAMP无效情况记录
    2021年12月21日复盘 雪花算法 服务器时钟偏移错误
    Centos替换源
    2021年12月9日复盘 前端日期少8小时
    WPF中DataContext作用
  • 原文地址:https://www.cnblogs.com/lzj-0218/p/3572375.html
Copyright © 2011-2022 走看看