• POJ-1088 Skiing(记忆化搜索)


    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

    Difficulty:

    1. 不一定从最高位置往下滑是最长长度(满足高度减小即可),而且高度可能出现相等的情况。所以要从每个点出发往下搜。

    2.记忆化搜索:记忆化。状态转移方程。 

    动态规划的方法,首先想个状态,dp[i][j]表示到达(i,j)这个点还能划多远,向四个方向转移,状态确实蛮好的,可是转移就有一点困难了。
    于是想到记忆化搜索,状态不变,用递归来转移。
    动态规划的方程:
    dp[i][j]=max(dp[i-1][j],dp[i+1][j],dp[i][j-1],dp[i][j+1])+1; (i,j)比其他点高
    现在的方程:
    dp[i][j]=max(dfs(i-1,j),dfs(i+1,j),dfs(i,j-1),dfs(i,j+1))+1; (i,j)比其他点高

    #include <cstdio>
    #include <iostream>
    #include <cstdlib>
    #include <algorithm>
    #include <ctime>
    #include <cmath>
    #include <string>
    #include <cstring>
    #include <stack>
    #include <queue>
    #include <list>
    #include <vector>
    #include <map>
    #include <set>
    using namespace std;
    
    const int INF=0x3f3f3f3f;
    const double eps=1e-10;
    const double PI=acos(-1.0);
    #define maxn 1100
    int r,c;
    int dx[4] = {0, 0, -1, 1};
    int dy[4] = {1, -1, 0, 0};
    int pic[maxn][maxn];
    int dp[maxn][maxn];
    int dfs(int sx, int sy)
    {
    
        if(dp[sx][sy] != -1) return dp[sx][sy];
        int temp = 0;
        for(int i = 0; i < 4; i++)
        {
            int nx = sx + dx[i];
            int ny = sy + dy[i];
            if(nx >= 0 && nx < r && ny >= 0 && ny < c)
            {
                if(pic[nx][ny] < pic[sx][sy])
                temp = max(temp, dfs(nx, ny)+1);
            }//printf("%d
    ", temp);
        }
    
        dp[sx][sy] = temp;
        return temp;
    }
    int main()
    {
        while(~scanf("%d%d", &r, &c))
        {
    
            int h_max = -1;
            memset(dp, -1, sizeof dp);
            for(int i = 0; i < r; i++)
                for(int j = 0; j < c; j++)
                {
                    scanf("%d", &pic[i][j]);
                    h_max = max(h_max, pic[i][j]);
                }
                //printf("%d$$$$$", h_max);
                int tea = -1;
                for(int i = 0; i < r; i++)
                    for(int j = 0; j < c; j++)
                    {
                            tea = max(tea, dfs(i,j)+1);
                    }
                printf("%d
    ", tea);
        }
        return 0;
    }
    /*
    3 3
    1 2 3
    7 8 9
    6 5 4
    
    */
  • 相关阅读:
    C#
    C#
    css
    css
    css
    css
    css
    Css
    Javascript
    ASP.NET MVC
  • 原文地址:https://www.cnblogs.com/ZP-Better/p/4639608.html
Copyright © 2020-2023  润新知