zoukankan      html  css  js  c++  java
  • 洛谷P1434 [SHOI2002]滑雪

    https://www.luogu.org/problem/P1434 记忆化搜索

    1.

    #include<bits/stdc++.h>
    using namespace std ;
    int n,m;
    const int N=10010;
    int a[N][N],s[N][N];
    int ans;
    int dx[4]= {0,0,1,-1};
    int dy[4]= {1,-1,0,0};
    int dfs(int x,int y) {
        if(s[x][y]) return s[x][y];
        s[x][y]=1;
        for(int i=0; i<4; i++) {
            int xx=dx[i]+x;
            int yy=dy[i]+y;//四个方向
            if(xx>0&&yy>0&&xx<=n&&yy<=m&&a[x][y]>a[xx][yy]) {
                dfs(xx,yy);
                s[x][y]=max(s[x][y],s[xx][yy]+1);
            }
        }
        return s[x][y];
    }
    int main() {
        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++)
                ans=max(ans,dfs(i,j));
        cout<<ans;
        return 0;
    }

    2.

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn = 200;
    const int dx[]= {0,0,1,-1};
    const int dy[]= {1,-1,0,0};
    int r,c,ans;
    int a[maxn][maxn],step[maxn][maxn];
    int dfs(int x,int y) {
        if(step[x][y]) return step[x][y];
        step[x][y]=1;
        for(int i=0; i<4; i++) {
            int nx=x+dx[i],ny=y+dy[i];
            if(a[nx][ny]<a[x][y]) {
                step[x][y]=max(step[x][y],1+dfs(nx,ny));
            }
        }
        return step[x][y];
    }
    int main() {
        cin>>r>>c;
        for(int i=0; i<=c+1; i++)  //设置边界
            a[i][0]=a[i][c+1]=1e9;
        for(int i=0; i<=r+1; i++)
            a[0][i]=a[r+1][i]=1e9;
        for(int i=1; i<=r; i++)
            for(int j=1; j<=c; j++)
                scanf("%d",&a[i][j]);
        for(int i=1; i<=r; i++)
            for(int j=1; j<=c; j++)
                ans=max(dfs(i,j),ans);
        cout<<ans;
        return 0;
    }
  • 相关阅读:
    G1(Garbage First)
    优化老年代参数的分析思路
    优化年轻代参数的分析思路
    频繁发生fullgc的原因和定位思路
    什么时候会触发老年代gc
    老年代回收器cms
    SQL的优化技巧
    常用前端性能优化
    刷新的方式
    遇到的一些坑
  • 原文地址:https://www.cnblogs.com/QingyuYYYYY/p/11777890.html
Copyright © 2011-2022 走看看