P1434 [SHOI2002]滑雪

本文介绍了一种求解二维地形中从任意点出发的最长下降路径的算法。通过使用深度优先搜索结合动态规划,算法能够高效地找到从每个点出发的最长下降路径。文章详细解释了算法的实现过程,并提供了完整的C++代码示例。
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int dx[4]={0,1,0,-1};
const int dy[4]={1,0,-1,0};//四个方向
int x,y;
int tot=1,sum[10001]={0},ans=0;
struct data
{
    int x,y,h;
}a[10001];//插歪坐标和高度
bool cmp(data a,data b)
{
    return a.h>b.h;
}//比较高度
bool check(int i,int j)
{
    int k;
    for(k=0;k<=3;k++)//四方向试探
    {
        if(a[i].x+dx[k]==a[j].x && a[i].y+dy[k]==a[j].y)
        {
            return true;
        }
    }
    return false;
}
int main()
{
    int i=0,j=0;
    cin>>x>>y;
    for(i=1;i<=x;i++)
    {
        for(j=1;j<=y;j++)
        {
            cin>>a[tot].h;
            a[tot].x=i;
            a[tot].y=j;
            tot++;
        }
    }
    sort(a+1,a+1+tot,cmp); 
    for(i=1;i<=tot;i++)
    {
      sum[i]=1;
        for (j=i-1;j>0;j--)
        {
          if (check(i,j)&&a[j].h>a[i].h)
           sum[i]=max(sum[i],sum[j]+1);//DP
        }
    }
    ans=0;
    for(i=1;i<=tot;i++)
    {
        ans=max(ans,sum[i]);
    }
    cout<<ans<<endl;
    return 0;
}

转载于:https://www.cnblogs.com/LSWorld/p/9704035.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值