E - 滑雪
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
思路:把每一点的信息用结构体存储,按高度从小到大排序,然后对结构体数组进行遍历,用dp[x][y]表示从点(x,y)出发的最长滑落距离,假如现在遍历到第i个点,那么该点上下左右中比该点低的点的情况已近计算过,比该点高的点对求该求从该点出发的最长滑落距离无关,那么慢只要找到比该点低点中的最大的dp值,然后+1就是该点的dp值,注意如果周围的点都比该点高,需要置该点的dp值为1,表示只能滑落一个距离长度。
#include<cstdio>
#include<stack>
#include<set>
#include<vector>
#include<queue>
#include<algorithm>
#include<cstring>
#include<string>
#include<map>
#include<iostream>
#include<cmath>
using namespace std;
#define inf 0x3f3f3f3f
typedef long long ll;
const int N=110;
const int nmax = 23;
const double esp = 1e-9;
const double PI=3.1415926;
int a[N][N],dp[N][N];
int dir[4][2]= {{-1,0},{1,0},{0,-1},{0,1}};
struct point
{
int x,y,val;
} p[N*N];
bool cmp(point p1,point p2)
{
return p1.val<p2.val;
}
int n,m;
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
int k=0;
for(int i=0; i<n; i++)
{
for(int j=0; j<m; j++)
{
scanf("%d",&a[i][j]);
p[k].x=i;
p[k].y=j;
p[k++].val=a[i][j];
}
}
sort(p,p+k,cmp);
memset(dp,0,sizeof(dp));
int maxl=0;
for(int i=0; i<k; i++)
{
int flag=1;
for(int j=0; j<4; j++)
{
int xx=p[i].x+dir[j][0];
int yy=p[i].y+dir[j][1];
if(xx>=0&&xx<n&&yy>=0&&yy<m&&p[i].val>a[xx][yy]) //搜索上下左右四个点
{
dp[p[i].x][p[i].y]=max(dp[p[i].x][p[i].y],dp[xx][yy]+1);
flag=0;
}
}
if(flag)
dp[p[i].x][p[i].y]=1;
maxl=max(maxl,dp[p[i].x][p[i].y]);
}
printf("%d\n",maxl);
}
return 0;
}