OpenJudge/Poj 1088 滑雪

1.链接地址:

bailian.openjudge.cn/practice/1088

http://poj.org/problem?id=1088

2.题目:

总Time Limit:
1000ms
Memory Limit:
65536kB
Description
Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激。可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你。Michael想知道载一个 区域中最长的滑坡。区域由一个二维数组给出。数组的每个数字代表点的高度。下面是一个例子
 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

一个人可以从某个点滑向上下左右相邻四个点之一,当且仅当高度减小。在上面的例子中,一条可滑行的滑坡为24-17-16-1。当然25-24-23-...-3-2-1更长。事实上,这是最长的一条。
Input
输入的第一行表示区域的行数R和列数C(1 <= R,C <= 100)。下面是R行,每行有C个整数,代表高度h,0<=h<=10000。
Output
输出最长区域的长度。
Sample Input
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
25
Source
Don't know

3.思路:

动态规划,先按照高度降序排序,再依次计算

刚开始想当然,以为从最高高度寻找一个路径一定是最长,所以使用了优先队列+广搜,白白WA了一次

4.代码:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cstdlib>
 5 
 6 using namespace std;
 7 
 8 struct PATH
 9 {
10     int x;
11     int y;
12     int height;
13 };
14 
15 int cmp(const void* a,const void *b)
16 {
17     PATH *p1 = (PATH *) a;
18     PATH *p2 = (PATH *) b;
19     return p2->height - p1->height;
20 }
21 
22 int main()
23 {
24     //freopen("C://input.txt","r",stdin);
25 
26     int r,c;
27     cin >> r >> c;
28 
29     int i,j;
30 
31     int **arr_height = new int*[r];
32     for(i = 0; i < r; ++i) arr_height[i] = new int[c];
33 
34     int **arr_mark = new int*[r];
35     for(i = 0; i < r; ++i)
36     {
37         arr_mark[i] = new int[c];
38         memset(arr_mark[i],0,sizeof(int) * c);
39     }
40 
41     PATH *arr_path = new PATH[r * c];
42 
43     for(i = 0; i < r; ++i)
44     {
45         for(j = 0; j < c; ++j)
46         {
47             cin >> arr_height[i][j];
48             arr_path[i * c + j].x = j;
49             arr_path[i * c + j].y = i;
50             arr_path[i * c + j].height = arr_height[i][j];
51         }
52     }
53 
54     qsort(arr_path,r * c,sizeof(PATH),cmp);
55 
56     int idx_x[] = {-1,1,0,0};
57     int idx_y[] = {0,0,1,-1};
58 
59     int res = 0;
60     for(i = 0; i < r * c; ++i)
61     {
62         //cout << arr_path[i].height << " " << arr_path[i].x << " " << arr_path[i].y << endl;
63 
64         int max = 0;
65         for(j = 0; j < 4; ++j)
66         {
67             int temp_x = arr_path[i].x + idx_x[j];
68             int temp_y = arr_path[i].y + idx_y[j];
69 
70             if(temp_x < 0 || temp_x >= c || temp_y < 0 || temp_y >= r) continue;
71 
72             if(arr_height[temp_y][temp_x] > arr_height[arr_path[i].y][arr_path[i].x] && max < arr_mark[temp_y][temp_x])
73             {
74                 max = arr_mark[temp_y][temp_x];
75             }
76         }
77 
78         arr_mark[arr_path[i].y][arr_path[i].x] = max + 1;
79         if(res < max + 1) res = max + 1;
80     }
81 
82     cout << res << endl;
83 
84     delete [] arr_path;
85 
86     for(i = 0; i < r; ++i) delete [] arr_height[i];
87     delete [] arr_height;
88 
89     for(i = 0; i < r; ++i) delete [] arr_mark[i];
90     delete [] arr_mark;
91 
92     return 0;
93 }
原文地址:https://www.cnblogs.com/mobileliker/p/3584105.html