滑雪
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 91779 | Accepted: 34673 |
Description
Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激。可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你。Michael想知道载一个区域中最长底滑坡。区域由一个二维数组给出。数组的每个数字代表点的高度。下面是一个例子
一个人可以从某个点滑向上下左右相邻四个点之一,当且仅当高度减小。在上面的例子中,一条可滑行的滑坡为24-17-16-1。当然25-24-23-...-3-2-1更长。事实上,这是最长的一条。
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
思路:
比较裸的搜索,也可以看作是dp,转移方程为 dp[i][j] = max(dp[可以到达的点]) + 1; 然后从最高点从上往下进行dp。
#include <iostream>
#include <iomanip>
#include <queue>
#include <stack>
#include <cstdio>
#include <cstring>
#include <map>
#include <cmath>
#include <algorithm>
#define INF 0x3f3f3f3f
using namespace std;
int arr[110][110];
int oc[110][110];
bool judge[110][110];
int move[4][2] = {1,0, -1, 0, 0, 1, 0, -1};
struct node{
int x, y;
};
bool operator<(const node& a, const node& b){
return arr[a.x][a.y] > arr[b.x][b.y];
}
int main()
{
int m, n, i, j, maxn = 0;
scanf("%d%d", &m, &n);
for(i = 0; i <= m; i++)
arr[i][0] = arr[i][n+1] = INF;
for(i = 0; i <= n; i++)
arr[0][i] = arr[m + 1][i] = INF;
arr[m + 1][n + 1] = INF;
priority_queue<node> que;
node temp, next;
for(i = 1; i <= m; i++)
for(j = 1; j <= n; j++){
scanf("%d", &arr[i][j]);
temp.x = i; temp.y = j;
que.push(temp);
judge[i][j] = 1;
}
while(!que.empty()){
temp = que.top();
que.pop();
judge[temp.x][temp.y] = 1;
for(i = 0; i < 4; i++){
next.x = temp.x + move[i][0];
next.y = temp.y + move[i][1];
if(arr[temp.x][temp.y] > arr[next.x][next.y]
&& oc[temp.x][temp.y] < oc[next.x][next.y] + 1){
oc[temp.x][temp.y] = oc[next.x][next.y] + 1;
maxn = max(oc[temp.x][temp.y], maxn);
if(!judge[next.x][next.y]) que.push(next);
}
}
}
cout << maxn + 1 << endl;
return 0;
}