洛谷 P1434 [SHOI2002] 滑雪(dfs+记忆化搜索)

https://www.luogu.com.cn/problem/P1434

题目大意:

找一条路径,从高到低,遍历过的地点最长。
输入 #1 
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
输出 #1 
25
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL,LL> PII;
const LL MAXN=1e18;
const LL N=500200,M=2002;
int n,m,maxn=0,a[M][M],mp[M][M];
//mp存储比自己小的地方的数量
int dx[]={-1,0,0,1},dy[]={0,-1,1,0};
int dfs(int x,int y)
{
    if(mp[x][y]) return mp[x][y];//如果已经标记过,直接返回
    //没有标记过的话,这个地方是第一个点,默认是最小的地点
    mp[x][y]=1;
    for(int i=0;i<4;i++)//四个方向对比
    {
        int xx=dx[i]+x,yy=dy[i]+y;
        if(xx>=1&&xx<=n&&yy>=1&&yy<=m&&a[x][y]>a[xx][yy])//因为找的地方是更小的,所以直接是>
        {
            dfs(xx,yy);//进入这个点往下搜索
            mp[x][y]=max(mp[x][y],mp[xx][yy]+1);//到底是自己更大,还是我往下的那一条带上自己更大
        }
    }
    return mp[x][y];//返回值
}
int main()
{
    cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
    LL T=1;
    //cin>>T;
    while(T--)
    {
        maxn=0;
        cin>>n>>m;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                cin>>a[i][j];
            }
        }
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                maxn=max(maxn,dfs(i,j));//遍历,从这个点往下寻找比自己小的地方
            }
        }
        cout<<maxn<<endl;
    }
    return 0;
}
posted @ 2022-11-01 10:56  高尔赛凡尔娟  阅读(39)  评论(0编辑  收藏  举报