UVa 10285 Longest Run on a Snowboard

Problem Cios

Longest Run on a Snowboardide

Input: standard inputthis

Output: standard outputspa

Time Limit: 5 secondscode

Memory Limit: 32 MBblog

 

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.ci

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:input

 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.string

Inputit

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++]
相關文章
相關標籤/搜索