2014-08-02 16:08:38
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 100, N 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
思路:简单的记忆化搜索,就当入门。
1 /************************************************************************* 2 > File Name: d.cpp 3 > Author: Nature 4 > Mail: 564374850@qq.com 5 > Created Time: Wed 30 Jul 2014 01:56:54 PM CST 6 ************************************************************************/ 7 8 #include <cstdio> 9 #include <cstring> 10 #include <cstdlib> 11 #include <cmath> 12 #include <iostream> 13 #include <algorithm> 14 #include <string> 15 using namespace std; 16 17 int g[105][105]; 18 int dp[105][105]; 19 int Case; 20 int R,C; 21 int ans; 22 string name; 23 24 int Search(int x,int y){ 25 if(dp[x][y]) 26 return dp[x][y]; 27 int t1,t2,t3,t4; 28 t1 = t2 = t3 = t4 = 0; 29 //up 30 if(x > 0 && g[x - 1][y] < g[x][y]) 31 t1 = Search(x - 1,y); 32 //right 33 if(y < C - 1 && g[x][y + 1] < g[x][y]) 34 t2 = Search(x,y + 1); 35 //down 36 if(x < R - 1 && g[x + 1][y] < g[x][y]) 37 t3 = Search(x + 1,y); 38 //left 39 if(y > 0 && g[x][y - 1] < g[x][y]) 40 t4 = Search(x,y - 1); 41 dp[x][y] = 1 + max(t1,max(t2,max(t3,t4))); 42 return dp[x][y]; 43 } 44 45 int main(){ 46 scanf("%d",&Case); 47 while(Case--){ 48 cin >> name; 49 scanf("%d%d",&R,&C); 50 memset(dp,0,sizeof(dp)); 51 for(int i = 0; i < R; ++i) 52 for(int j = 0; j < C; ++j) 53 scanf("%d",&g[i][j]); 54 ans = 0; 55 for(int i = 0; i < R; ++i) 56 for(int j = 0; j < C; ++j) 57 ans = max(ans,Search(i,j)); 58 cout << name; 59 printf(": %d\n",ans); 60 } 61 return 0; 62 }
本文介绍了一种通过记忆化搜索解决最长雪道问题的方法。利用递归搜索算法,在给定地形高度图的情况下找出最长的下滑路径。算法遍历每个点并检查四个方向,确保每次移动都是向下倾斜的。
220

被折叠的 条评论
为什么被折叠?



