zoukankan      html  css  js  c++  java
  • POJ 1088 滑雪 记忆化DP

    滑雪
    Time Limit: 1000MS   Memory Limit: 65536K
         

    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

          可以说所有用递推实现的动态规划均可以利用记忆化搜索实现。而某些题目之所以可以用递推求解,是因为这类题目同一阶段的状态表示上有很大的相关性,比如数字矩阵中某一行或列,这使得我们可以计算一个阶段的所有状态后再计算下一状态。而某些题目中利用动态规划划分的同一阶段的状态表示上没有多大相关性,比如Skiing里面的状态,从某点做起点每滑动一步为一个阶段,我们无法用一个准确的可以直接利用的集合将一个阶段中的状态表示出来,只能从已知状态去找和它相关联的状态。对于Skiing我们已知的是目标状态(即四面都不比该点高的点),通过边界条件(即四面都比该点高的最优值为1),便可以进行记忆化搜索。

    #include <iostream>
    #include <string>
    #include <string.h>
    #include <map>
    #include <stdio.h>
    #include <algorithm>
    #include <queue>
    #include <vector>
    #include <math.h>
    #include <set>
    #define Max(a,b) ((a)>(b)?(a):(b))
    #define Min(a,b) ((a)<(b)?(a):(b))
    using namespace std ;
    const int d[4][2]={{1,0},{-1,0},{0,-1},{0,1}} ;
    int N ,M ;
    int height[108][108] ;
    int dp[108][108] ;
    
    int cango(int x ,int y){
        return 1<=x && x<=N && 1<=y && y<=M ;
    }
    
    int dfs(int x ,int y){
        if(dp[x][y] != -1)
          return dp[x][y] ;
        int all = 0 ;
        for(int i = 0 ; i < 4 ; i++){
            int nx = x + d[i][0] ;
            int ny = y + d[i][1] ;
            if(!cango(nx,ny))
              continue  ;
            if(height[x][y] > height[nx][ny])
               all = Max(all ,1 + dfs(nx,ny)) ;
        }
        if(all == 0)
          return dp[x][y] = 1 ;
        else
          return dp[x][y] = all ;
    }
    
    int main(){
       while(scanf("%d%d",&N,&M)!=EOF){
            for(int i = 1 ; i <= N ; i++)
               for(int j = 1 ; j <= M ; j++)
                  scanf("%d",&height[i][j]) ;
            memset(dp,-1,sizeof(dp)) ;
            int ans = 0 ;
            for(int i = 1 ; i <= N ; i++)
               for(int j = 1 ; j <= M ; j++)
                  ans = Max(ans,dfs(i,j)) ;
            cout<<ans<<endl ;
       }
       return 0 ;
    }
  • 相关阅读:
    (转)前端开发框架选型清单
    (转)关于java和web项目中的相对路径问题
    (转)phonegap 数据库详解
    (转)SQLite数据库增删改查操作
    (转)JS中innerHTML,innerText,value
    (转)js函数参数设置默认值
    (转)HTML5开发学习(2):本地存储之localStorage 、sessionStorage、globalStorage
    (转)HTML5开发学习(3):本地存储之Web Sql Database
    [笔记]普通平衡树(Splay)
    [笔记][题解]树链剖分&lgP3384
  • 原文地址:https://www.cnblogs.com/liyangtianmen/p/3462509.html
Copyright © 2011-2022 走看看