POJ 1088 滑雪(记忆化搜索)

滑雪

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 92384   Accepted: 34948

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

题目链接:POJ 1088

做的第一道记忆化搜索的题目,每一次把最好的拓展结果记录下来再传回去,挺好理解的

代码:

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<bitset>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
using namespace std;
#define INF 0x3f3f3f3f
#define CLR(x,y) memset(x,y,sizeof(x))
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
typedef pair<int,int> pii;
typedef long long LL;
const double PI=acos(-1.0);
const int N=110;
int pos[N][N],direct[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
int dp[N][N];
int n,m;
int dfs(int x,int y)
{
    if(dp[x][y])
        return dp[x][y];
    else
    {
        int maxm=0;
        for (int i=0; i<4; ++i)
        {
            int xx=x+direct[i][0];
            int yy=y+direct[i][1];
            if(xx>=0&&xx<n&&yy>=0&&yy<m&&pos[xx][yy]<pos[x][y])
                maxm=max<int>(maxm,dfs(xx,yy));
        }
        return dp[x][y]=maxm+1;
    }
}
int main(void)
{
    int i,j;
    while (~scanf("%d%d",&n,&m))
    {
        CLR(dp,0);

        for (i=0; i<n; ++i)
            for (j=0; j<m; ++j)
                scanf("%d",&pos[i][j]);

        for (i=0; i<n; ++i)
            for (j=0; j<m; ++j)
                dfs(i,j);

        int ans=1;
        for (i=0; i<n; ++i)
            for (j=0; j<m; ++j)
                if(dp[i][j]>ans)
                    ans=dp[i][j];

        printf("%d\n",ans);
    }
    return 0;
}
时间: 2024-08-05 06:50:26

POJ 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,

poj 3156 hash+记忆化搜索 期望dp

#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> using namespace std; int n,m; #define N 32 #define mod 10007LL #define mod2 10000007 #define inf 0x3fffffff typedef long long ll; typedef double dd; int f[N]

POJ 4968 DP||记忆化搜索

给出N个人的平局分X 根据GPA规则计算可能的最高平均GPA和最低平均GPA 可以DP预处理出来所有结果  或者记忆化搜索 DP: #include "stdio.h" #include "string.h" int inf=100000000; double a[11][1100],b[11][1100]; double Max(double a,double b) { if (a<b) return b; else return a; } double M

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)//是否出