OpenJ_Bailian - 1088 滑雪(记忆化搜索)

题意:给定一个二维数组,一个人可以从某个点滑向上下左右相邻四个点之一,当且仅当高度减小,输出可以滑行的最长区域的长度。

分析:对于每一个点,进行记忆化搜索。若某点可以向四周某几个点滑行,记忆化搜索求出这几个可滑行点的最长滑行长度,取最大值,则该点的最长滑行长度为最大值+1.

注意:不能直接dp[x][y]=max(dp[x][y],dfs(tmpx,tmpy)+1),因为若四周没有可滑行的点,则该点最长滑行长度dp[x][y]会输出0,而实际dp[x][y]为1,分析中的方法可避免这个问题。

#include<cstdio>
#include<algorithm>
using namespace std;
const int MAXN = 100 + 10;
int pic[MAXN][MAXN];
int dp[MAXN][MAXN];
const int dr[] = {0, 0, 1, -1};
const int dc[] = {1, -1, 0, 0};
int R, C;
bool judge(int x, int y){
    return x >= 0 && x < R && y >= 0 && y <= R;
}
int dfs(int x, int y){
    if(dp[x][y]) return dp[x][y];
    int ans = 0;
    for(int i = 0; i < 4; ++i){
        int tmpx = x + dr[i];
        int tmpy = y + dc[i];
        if(judge(tmpx, tmpy) && pic[tmpx][tmpy] < pic[x][y]){
            ans = max(ans, dfs(tmpx, tmpy));
        }
    }
    return dp[x][y] = ans + 1;
}
int main(){
    scanf("%d%d", &R, &C);
    for(int i = 0; i < R; ++i){
        for(int j = 0; j < C; ++j){
            scanf("%d", &pic[i][j]);
        }
    }
    int ans = 0;
    for(int i = 0; i < R; ++i){
        for(int j = 0; j < C; ++j){
            ans = max(ans, dfs(i, j));
        }
    }
    printf("%d\n", ans);
    return 0;
}

  

原文地址:https://www.cnblogs.com/tyty-Somnuspoppy/p/8719647.html

时间: 2024-08-02 21:33:34

OpenJ_Bailian - 1088 滑雪(记忆化搜索)的相关文章

[ACM] poj 1088 滑雪 (记忆化搜索DFS)

求n*m网格内矩形的数目[ACM] poj 1088 滑雪 (记忆化搜索DFS),布布扣,bubuko.com

POJ 1088 滑雪(记忆化搜索+dfs)

滑雪 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 83489   Accepted: 31234 Description Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道载一个区域中最长底滑坡.区域由一个二维数组给出.数组的每个数字代表点的高度.下面是一个例子 1 2 3 4 5 16 17

POJ 1088 滑雪 记忆化搜索

解析:状态d[i][j]代表r=i , c=j这个位置能滑的最大长度.深搜+备忘录 #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> using namespace std; const int maxn=100+5; int R,C; int a[maxn][maxn]; int d[maxn][maxn]; int dr[]={0,-1,0,1};

POJ 1088滑雪 记忆化搜索

#include <cstdio> #include <iostream> #include <algorithm> #include <queue> #include <stack> #include <cstdlib> #include <cmath> #include <set> #include <map> #include <vector> #include <cstri

POJ 1088 滑雪 记忆化优化题解

本题有人写是DP,不过和DP还是有点差别的,应该主要是记忆化 Momoization 算法. 思路就是递归,然后在递归的过程把计算的结果记录起来,以便后面使用. 很经典的搜索题目,这种方法很多题目考到的. 关键还是如何把代码写清晰工整了,O(∩_∩)O~. #include <stdio.h> const int MAX_N = 101; int R, C; int arr[MAX_N][MAX_N]; int tbl[MAX_N][MAX_N]; inline int max(int a,

tyvj 1004 滑雪 记忆化搜索

滑雪 Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://www.tyvj.cn/p/1004 Description trs喜欢滑雪.他来到了一个滑雪场,这个滑雪场是一个矩形,为了简便,我们用r行c列的矩阵来表示每块地形.为了得到更快的速度,滑行的路线必须向下倾斜.    例如样例中的那个矩形,可以从某个点滑向上下左右四个相邻的点之一.例如24-17-16-1,其实25-24-23…3-2-1更长,事实上这是最长的一条. Input 输入文件

滑雪 记忆化搜索

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

poj1088 滑雪 记忆化搜索

我是渣渣 我是渣渣 ....记忆化搜索 看了学长的代码 和算法竞赛入门经典这本书才勉勉强强看懂点... #include <stdio.h> #include <string.h> int dir[4][2]={1,0,-1,0,0,1,0,-1};//方向 int dp[101][101],map[101][101]; int m,n; int max(int x,int y) { return x>y?x:y; } bool limit(int x,int y)//是否出

POJ 1088 滑雪(记忆化搜索)

滑雪 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 92384   Accepted: 34948 Description Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道载一个区域中最长底滑坡.区域由一个二维数组给出.数组的每个数字代表点的高度.下面是一个例子 1 2 3 4 5 16 17