zoukankan      html  css  js  c++  java
  • 滑雪 矩阵中的最长上升路径 /// 记忆化DFS || DP oj22919

    大致题意:

    Description

    难怪Michael喜欢滑雪,因为滑雪确实很刺激。为了获得加速度,滑雪道必须向下倾斜,而且当滑到坡底,你不得不再次走上坡或者等待升降机来载你。Michael想知道在一个区域中最长的滑坡。区域由一个二维数组给出。数组的每个数字代表点的高度。下面是一个例子

    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

    一个人可以从某个点滑向上下左右相邻四个点之一,当且仅当高度减小。在上面的例子中,一条可滑行的滑坡为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 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

    记忆化DFS 时间限制100ms内

    #include<bits/stdc++.h>
    using namespace std;
    int G[105][105],len[105][105],have[105][105];
    /// len[][]为当前点的最长路, have[][]为当前点已搜过具有最长路len[][]
    int n,m,mov[2][4]={0,0,1,-1,1,-1,0,0};
    bool bound(int p,int q) // 越界
    {
        return p>n||p<1||q>m||q<1;
    }
    int DFS(int p,int q)
    {
        if(have[p][q]) return len[p][q]; //已搜过的直接返回其最长路
        int ans=1;
        for(int i=0;i<4;i++)
        {
            int np=p+mov[0][i],nq=q+mov[1][i];
            if(bound(np,nq)) continue;
            if(G[p][q]>G[np][nq]) ans=max(ans,DFS(np,nq)+1);
        } /// 搜该点出发的四个点 取其中路最长的一个值+1
        len[p][q]=ans; /// 存入该点的最长路
        have[p][q]=1; /// 搜完四个点 则该点最长路已更新 标为已搜过
        return ans; 
    }
    int main()
    {
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
                scanf("%d",&G[i][j]);
        memset(have,0,sizeof(have));
        int ans=0;
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
            {
                len[i][j]=DFS(i,j); /// 深搜该点 得到其最长路
                ans=max(ans,len[i][j]);
            }
        printf("%d",ans);
    
        return 0;
    }
    View Code

    DP 时间限制1000ms内

    #include<bits/stdc++.h>
    using namespace std;
    int n,c,dp[105*105];
    struct NODE{ int x,y,l; }node[105*105];
    bool cmp(NODE a,NODE b){ return a.l>b.l; }
    bool cheak(int i,int j)
    {
        int a=fabs(node[i].x-node[j].x);
        int b=fabs(node[i].y-node[j].y);
        if(a+b==1) return 1;
        return 0;
    }
    int main()
    {
        scanf("%d%d",&n,&c);
        int len=0;
        for(int i=1;i<=n;i++)
            for(int j=1;j<=c;j++)
            {
                int m; scanf("%d",&m);
                node[len].x=i, node[len].y=j;
                node[len++].l=m;
            }
        sort(node,node+len,cmp);
        memset(dp,0,sizeof(dp));
        int ans=0;
        for(int i=0;i<len;i++)
        {
            for(int j=0;j<i;j++)
                if(cheak(i,j)&&node[j].l>node[i].l)
                    dp[i]=max(dp[i],dp[j]+1);
            ans=max(dp[i],ans);
        }
        printf("%d",ans+1);
    
        return 0;
    }
    View Code
  • 相关阅读:
    bash脚本入门
    DNS 递归查询
    场景题
    利用 python 发送邮件(qq 邮件)
    Swagger 简单使用
    Nginx支持HTTPS,生成SSL证书
    使用 Python 搭建简易HTTP服务器
    扫码登陆原理
    【积累】在jQuery.Validate额中使用可以传入参数的message
    关于RUBY处理多语言转字符编码的一点经验 nkf
  • 原文地址:https://www.cnblogs.com/zquzjx/p/9035614.html
Copyright © 2011-2022 走看看