uva 10285

 

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 R and 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

记忆化搜索,这恐怕是我目前做的最顺畅的DP,一开始一部分变量初始化不当,出不来结果,稍稍看了下题解,初始化的时候想多了(啥时候能不看题解写DP,前几次也是,公式找对了,就是初始化错了,死也出不来),对于DP中的状态和方向还是有点无感,悲催啊,弱渣只能跪啊!!!
 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <iostream>
 4 using namespace std;
 5 #define CL(x,y) memset(x,y,sizeof(x))
 6 const int MAXN = 105;
 7 const int inf = 10000000;
 8 int c ,r;
 9 int dp[MAXN][MAXN];
10 int map[MAXN][MAXN];
11 int vis[MAXN][MAXN];
12 int dir[4][2] = {{0,-1},{0,1},{-1,0},{1,0}};
13 
14 int DP(int i, int j)
15 {
16     if(dp[i][j]) return dp[i][j];
17 
18     int k;
19     int maxx = 0;
20     for(k = 0; k < 4; k ++)
21     {
22         int tmpx = i + dir[k][0];
23         int tmpy = j + dir[k][1];
24 
25         if(tmpx >= 1 && tmpx <= c && tmpy >= 1 && tmpy <= r && map[tmpx][tmpy] < map[i][j])
26         {
27             int t = DP(tmpx,tmpy);
28             if(t > maxx)
29                 maxx = t;
30         }
31     }
32 
33     dp[i][j] = maxx + 1;
34 
35     return dp[i][j];
36 }
37 
38 int main()
39 {
40     int t;
41     int i,j;
42     string name;
43 
44     cin >> t;
45     while(t --)
46     {
47         cin >> name >> c >> r;
48 
49         for(i = 1 ;i <= c ; i ++)
50             for(j = 1 ; j <= r ; j ++)
51                 cin >> map[i][j];
52 
53         CL(dp,0);
54         int maxx = 0;
55         for(i = 1 ; i <= c ; i ++)
56             for(j = 1 ; j <= r ; j ++)
57                     maxx = max(maxx,DP(i,j));
58 
59         cout << name <<": "<<maxx << endl;
60 
61     }
62 
63     return 0;
64 }

原文地址:https://www.cnblogs.com/coolwind-sea/p/3247792.html