POJ 1088 滑雪 (DPor记忆化搜索)

题目大意:中文题:http://poj.org/problem?id=1088

先上一发自己写的记忆化,渣,900多毫秒

#include <iostream>
#include <cstdio>
#include <stack>
#include <cstring>

using namespace std;

int a[111][111];
int visit[111][111];
int dp[111][111];
int dir[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};
int ans = 0;

void DFS(int x, int y, int s)
{
    if(dp[x][y]>=s)
        return ;
    visit[x][y]++;
    dp[x][y] = s;
    ans = max(ans, s);
    for(int i=0; i<4; i++)
    {
        int xx = x+dir[i][0];
        int yy = y+dir[i][1];
        if(visit[xx][yy] && a[xx][yy]>a[x][y])
        {
            DFS(xx, yy, s+1);
        }

    }
}

int main()
{
    int n, m;
    memset(visit, 0, sizeof(visit));
    while(~scanf("%d %d", &n, &m))
    {
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=m; j++)
            {
                scanf("%d", &a[i][j]);
                visit[i][j] = 1;
            }
        }
        for(int i=1; i<=n; i++)
            for(int j=1; j<=m; j++)
                if(visit[i][j]==1)
                DFS(i, j, 1);
        printf("%d\n", ans);
    }

    return 0;
}
/*
3 5
1 2 3 4 4
1 1 1 5 1
2 3 4 5 6
*/

渣渣

再看看他人的好的记忆化

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <stdlib.h>

using namespace std;

#define N 110
#define met(a, b) memset(a, b, sizeof(a))

typedef long long LL;

int n, m, dp[N][N], A[N][N], ans;
int dir[4][2] = { {-1, 0},{1, 0},{0, -1},{0, 1} };

int dfs(int x, int y)
{
    if(dp[x][y]) return dp[x][y];
    int Max = 0;
    for(int i=0; i<4; i++)
    {
        int px = x+dir[i][0];
        int py = y+dir[i][1];
        if(px>0 && py>0 && px<=m && py<=n && A[px][py]<A[x][y])
            Max = max(Max, dfs(px, py));
    }
    dp[x][y] = Max+1;
    ans = max(ans, dp[x][y]);
    return dp[x][y];
}

int main()
{
    while(scanf("%d %d", &m, &n)!=EOF)
    {
        met(A, 0);
        for(int i=1; i<=m; i++)
        {
            for(int j=1; j<=n; j++)
                scanf("%d", &A[i][j]);
        }
        met(dp, 0);

        ans = 0;

        for(int i=1; i<=m; i++)
        {
            for(int j=1; j<=n; j++)
            {
                dfs(i, j);
            }
        }
        printf("%d\n", ans);
    }
    return 0;
}

厉害

再看看用DP式写的

先进行点的排序

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <stdlib.h>

using namespace std;

#define N 110
#define met(a, b) memset(a, b, sizeof(a))

typedef long long LL;

int n, m, dp[N][N], A[N][N];
int dir[4][2] = { {-1, 0},{1, 0},{0, -1},{0, 1} };

struct node
{
    int x, y, w;
    friend bool operator<(node p, node q)
    {
        return p.w < q.w;
    }
}a[N*N];

int main()
{
    while(scanf("%d %d", &m, &n)!=EOF)
    {
        met(a, 0);
        met(A, 0);
        int len = 0;
        for(int i=1; i<=m; i++)
        {
            for(int j=1; j<=n; j++)
            {
                scanf("%d", &A[i][j]);
                a[len].x = i;
                a[len].y = j;
                a[len++].w = A[i][j];
            }
        }
        sort(a, a+len);
        met(dp, 0);
        int ans = 0;
        for(int i=0; i<len; i++)
        {
            int Max = 0;
            for(int j=0; j<4; j++)
            {
                int p = a[i].x + dir[j][0];
                int q = a[i].y + dir[j][1];
                if(A[p][q] < a[i].w )
                    Max = max(Max, dp[p][q]);
            }
            dp[a[i].x][a[i].y] = Max + 1;
            ans = max(ans, Max+1);
        }
        printf("%d\n", ans);
    }
    return 0;
}
/*
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
*/

膜拜

时间: 2024-08-15 06:37:50

POJ 1088 滑雪 (DPor记忆化搜索)的相关文章

poj 1088 滑雪 【记忆化搜索】+【DFS】

策略:如题 题目链接:http://poj.org/problem?id=1088 代码: #include<stdio.h> #include<string.h> int map[105][105], dp[105][105], n, m; const int dir[4][2] = {0, 1, 1, 0, 0, -1, -1, 0}; //四个方向 int limit(int x, int y) //判断是不是越界了 { if(x<1||x>n||y<1||

POJ 1088 滑雪(记忆化搜索)

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

[ACM] POJ 1088 滑雪 (记忆化搜索复习)

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

poj 1088 滑雪【记忆化搜索】

题目链接:http://poj.org/problem?id=1088 基础题,不讲了,饿了 吃早饭去... 代码: #include <stdio.h> #include <iostream> #include <algorithm> #include <string.h> #include <queue> #include <math.h> #include <map> #include <string>

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

题意:给定一个二维数组,一个人可以从某个点滑向上下左右相邻四个点之一,当且仅当高度减小,输出可以滑行的最长区域的长度. 分析:对于每一个点,进行记忆化搜索.若某点可以向四周某几个点滑行,记忆化搜索求出这几个可滑行点的最长滑行长度,取最大值,则该点的最长滑行长度为最大值+1. 注意:不能直接dp[x][y]=max(dp[x][y],dfs(tmpx,tmpy)+1),因为若四周没有可滑行的点,则该点最长滑行长度dp[x][y]会输出0,而实际dp[x][y]为1,分析中的方法可避免这个问题. #

poj 1695 Magazine Delivery 记忆化搜索

dp[a][b][c],表示三个人从小到大依次在a,b,c位置时,距离结束最少的时间. 每次选一个人走到c+1位置搜索就好了. 坑点在于不能floyd,估计题目没说清楚,意思就是如果没送Li,那么Li~n的点连去都不能去. #include<cstdio> #include<queue> #include<algorithm> #include<cstring> using namespace std; #define INF 0x3f3f3f3f int

POJ 2738 Two Ends 记忆化搜索

题意比较简单,两个人拿偶数张卡片,要么拿当前的最左边,要么拿最右边,尽量拿大的,拿完后求第一个人拿的卡片数字总和减去第二个人卡片的数字总和,求最大差值. 刚开始写记忆化搜索,参考了一下别人的代码,但是还有一个地方不太明白,就是当第一个人拿右边的时候,为什么写成num[x]<=num[y-1]会wa(⊙o⊙)- 求指教额... #include <iostream> #include<stdio.h> #include<algorithm> #include<

poj1088 滑雪(记忆化搜索)

D - 滑雪 Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 1088 Appoint description: System Crawler (2016-05-09) Description Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获

poj1088滑雪(dfs+记忆化搜索、备忘录)

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

【题解】滑雪 luogu1434 记忆化搜索

记忆化搜索入门题 题目 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 一个人可以从某个点滑向上下左右相邻四个点之一,当且仅当高度减小. 在上面的例