POJ 1088-滑雪(dp)

来源:互联网 发布:无线图像传输 单片机 编辑:程序博客网 时间:2024/04/30 23:42

滑雪
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 79309 Accepted: 29502

Description

Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激。可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你。Michael想知道载一个区域中最长底滑坡。区域由一个二维数组给出。数组的每个数字代表点的高度。下面是一个例子 
 1  2  3  4 516 17 18 19 615 24 25 20 714 23 22 21 813 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 51 2 3 4 516 17 18 19 615 24 25 20 714 23 22 21 813 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;}




0 0
原创粉丝点击