POJ 1088 滑雪

滑雪
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 56474   Accepted: 20493

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

 
 
我维护了一个元素是pair<int,int>的最小优先度队列,pair的两个元素分别是高度在数组中所处的位置,然后逐个弹出最小优先度的元素,对该元素四边进行搜索,选取最大的一个加1后赋给dp数组。最后扫描dp数组,最大值+1即是所求。
 
 1 #include <iostream>
 2 #include <string>
 3 #include <stack>
 4 #include <queue>
 5 #include <vector>
 6 #include <cstdlib>
 7 #include <cstdio>
 8 #include <functional>
 9 #include <climits>
10 
11 using namespace std;
12 
13 int map[100][100];
14 int dp[100][100];
15 int dx[4] = {0,-1,0,1},dy[4] = {-1,0,1,0};
16 
17 class cmp
18 {
19 public:
20     bool operator() (const pair<int,int> &a,const pair<int,int> &b)
21     {
22         return a.first > b.first;
23     }
24 };
25 
26 int main(void)
27 {
28     priority_queue<pair<int,int>,vector<pair<int,int> >,cmp> q;
29     int row,col;
30     cin >> row >> col;
31     for(int i(0);i != row;++i)
32         for(int j(0);j != col;++j)
33         {
34             cin >> map[i][j];
35             q.push(make_pair(map[i][j],i*row+j));
36         }
37     int max = -INT_MAX;
38     while(!q.empty())
39     {
40         int y = (q.top().second)%row;
41         int x = (q.top().second)/row;
42         for(int i(0);i != 4;++i)
43         {
44             if(x+dx[i] >= 0 && x+dx[i] <= row-1 && y+dy[i] >= 0 && y+dy[i] <= col-1)
45                 if(map[x+dx[i]][y+dy[i]] < map[x][y])
46                     if(dp[x][y] < dp[x+dx[i]][y+dy[i]]+1)
47                         dp[x][y] = dp[x+dx[i]][y+dy[i]]+1;
48         }
49         q.pop();
50     }
51     for(int i(0);i != row;++i)
52         for(int j(0);j != col;++j)
53             if(dp[i][j] > max)
54                 max = dp[i][j];
55     cout << max+1 << endl;
56 
57     return 0;
58 }

 

 
posted @ 2012-04-25 10:41  gluowei39  阅读(194)  评论(0编辑  收藏  举报