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 9Sample Output
25
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<queue>
using namespace std;
typedef long long ll;
#define N 101
struct node {
int x;
int i,j;
friend bool operator<(node n,node m){
return n.x>m.x;//在优先队列里面,不等号相反
}
};
int next_s[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
int aa[N][N],dp[N][N];
int main(){
priority_queue<node>q;
int r,c;
cin>>r>>c;
for(int i=0;i<r;i++){
for(int j=0;j<c;j++){
dp[i][j]=1;
cin>>aa[i][j];
node t;
t.x=aa[i][j];//维护坐标和权值
t.i=i;
t.j=j;
q.push(t);
}
}
//最终各点按照高度从小到大排好序存在优先队列中
//求最长上升子序列的必要操作,排序完成
while(!q.empty()){//广搜
node s=q.top();
q.pop();
for(int k=0;k<4;k++){
int tx=s.i+next_s[k][0];
int ty=s.j+next_s[k][1];
if(tx<0||tx>=r||ty<0||ty>=c){
continue;
}
if(s.x>aa[tx][ty]){//线性dp最长子序列的公式
dp[s.i][s.j]=max(dp[s.i][s.j],dp[tx][ty]+1);
}
}
}
int k=0;
for(int i=0;i<r;i++){
for(int j=0;j<c;j++){
if(k<dp[i][j])
k=dp[i][j];
}
}
cout<<k<<endl;
return 0;
}