标签:
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 79309 | Accepted: 29502 |
Description
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
Input
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
题意摆在那,上来咱说说思路。要求的滑坡是一条节点递减并依次相邻的最长路径,首先你可以根据高度将所有的点从小到大排序,当然在之前要记录下所有点的横纵坐标,然后每到一个点,你找比当前点小的点,只需要找在自己前面的点就好了,然后将你前面的点和你当前点的上下左右四点的坐标做比较,找出比当前点小的在你当前点的四周的点的最大点,然后记录步数就好了。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#include <set>
#include <queue>
using namespace std;
const int inf=0x3f3f3f3f;
struct node
{
int x,y;//记录点的横纵坐标
int h;//记录点的高度
int step;//记录以当前的为终点所走的步数。
}dp[10010];
int cmp(struct node a,struct node b)
{
return a.h<b.h;
}
int main()
{
int n,m,i,j;
int cnt;
int high;
int res;
while(~scanf("%d %d",&n,&m)){
memset(dp,0,sizeof(dp));
cnt=0;
res=1;
for(i=0;i<n;i++)
for(j=0;j<m;j++){
scanf("%d",&high);
dp[cnt].h=high;
dp[cnt].x=i;
dp[cnt].y=j;
dp[cnt].step=1;
cnt++;
}
sort(dp,dp+cnt,cmp);
for(i=1;i<cnt;i++){
int xi=dp[i].x;
int yi=dp[i].y;
for(j=0;j<i;j++){
int xj=dp[j].x;
int yj=dp[j].y;
if((((xi==xj-1)&&(yi==yj))||((xi==xj)&&(yi==yj+1))||((xi==xj+1)&&(yi==yj))||((xi==xj)&&(yi==yj-1)))&&(dp[j].h<dp[i].h)){
dp[i].step=max(dp[i].step,dp[j].step+1);
res=max(res,dp[i].step);
}
}
}
printf("%d\n",res);
}
return 0;
}
标签:
原文地址:http://blog.csdn.net/u013486414/article/details/43235387