zoukankan      html  css  js  c++  java
  • POJ 1088: 滑雪(经典 DP+记忆化搜索)

    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 74996   Accepted: 27818

    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


    中文题什么的再也不用操心题目都看不懂了。

    。23333


    #include<algorithm>
    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<vector>
    #include<queue>
    #include<cmath>
    
    using namespace std;
    
    const int M = 105;
    int n, m;
    int map[M][M];
    int ans[M][M];
    int dx[] = {1, -1, 0, 0};
    int dy[] = {0, 0, -1, 1};
    
    int dp(int x, int y)
    {
        int max = 0;
        if( ans[x][y]>0 )
            return ans[x][y];
        for(int i=0; i<4; i++)  //四个方向
        {
            int xx = x + dx[i];
            int yy = y + dy[i];
            if( xx>=1 &&xx<=n &&yy>=1 &&yy<=m )  //边界
            {
                if( map[x][y] > map[xx][yy] )    //从高到低才合法
                if ( max < dp( xx, yy ) )
                    max = dp( xx, yy );
            }
        }
        return ans[x][y] = max + 1;
    }
    
    int main()
    {
        while( scanf( "%d%d", &n, &m ) !=EOF )
        {
            memset( map, 0, sizeof(map) );
            memset( ans, 0, sizeof(ans) );
            for( int i=1; i<=n; i++ )
                for( int j=1; j<=m; j++ )
                    scanf( "%d", &map[i][j] );
            for( int i=1; i<=n; i++ )
                for( int j=1; j<=m; j++ )
                    dp( i, j );
            for(int i=1; i<=n; i++)
                for(int j=1; j<=m; j++)
                    if( ans[1][1] < ans[i][j] )
                        ans[1][1] = ans[i][j];
            printf("%d
    ", ans[1][1]);
        }
    
        return 0;
    }
    





  • 相关阅读:
    ApacheCN JavaScript 译文集 20211122 更新
    ApacheCN jQuery 译文集 20211121 更新
    ApacheCN 大数据译文集(二) 20211206 更新
    我这里一直空荡荡的,还占着博客园的位置,真是不好意思
    2009年最后一天
    学习petshop的朋友。这里有一些讲坐下载
    Repeater控件中如何做编辑和删除功能
    可以让你少奋斗十年的工作经验
    GridView自定义分页,模仿GridView的自动分页功能
    从内容面访问母版页中的控件或属性
  • 原文地址:https://www.cnblogs.com/gccbuaa/p/7044173.html
Copyright © 2011-2022 走看看