HDU5024Wang Xifeng's Little Plot(记忆化搜索)

HDU5024Wang Xifeng‘s Little Plot(记忆化搜索)

题目链接

题目大意:给一张地图,#代表不能走的位置,.代表可以走的位置。现在要求找一条最长的路径,并且拐弯最多只能有一个并且还要是90度的。

解题思路:记忆化搜索,dp[x][y][k][d] : x, y 代表坐标,k代表拐了k次90弯,d代表方向。因为这里最多只能转一次弯,而且还必须是90度的,那么就不可能走重复的路了。

代码:

#include <cstdio>
#include <cstring>
#include <queue>
#include <cstdlib>

using namespace std;

const int N = 105;
const int M = 8;
const int dir[M][2] = {{-1, 0},{-1, 1}, {0, -1}, {1, 1},
                       {1, 0}, {1, -1}, {0, -1}, {-1, -1}};

char rec[N][N];

int n;
int f[N][N][M][2];

void init () {

    for (int i = 0; i <= n; i++)
        for (int j = 0; j <= n; j++)
            for (int d = 0; d < M; d++)
                for (int k = 0; k < 2; k++)
                    f[i][j][d][k] = -1;
}

int dp (int x, int y, int k, int d) {

    if (f[x][y][d][k] != -1)
        return f[x][y][d][k];

    int nx, ny;
    for (int i = 0; i < M; i++) {

        nx = x + dir[i][0];
        ny = y + dir[i][1];
        if (nx < 0 || nx >= n || ny < 0 || ny >= n)
            continue;
        if (rec[nx][ny] == ‘#‘)
            continue;
        if (i == d)
            f[x][y][d][k] = max (dp(nx, ny, k, d) + 1, f[x][y][d][k]);
        else if (!k && (abs(i - d) == 2 || abs (i - d) == 6))
            f[x][y][d][k] = max (dp(nx, ny, k + 1, i) + 1, f[x][y][d][k]);
    }

    if (f[x][y][d][k] == -1)
        f[x][y][d][k] = 1;
    return f[x][y][d][k];
}

int main () {

    while (scanf ("%d", &n) && n) {

        for (int i = 0; i < n; i++)
            scanf ("%s", rec[i]);

        int ans = -1;
        init();
        for (int i = 0; i < n; i++)
            for (int j = 0; j < n; j++) {
                if (rec[i][j] == ‘.‘) {
                    for (int d = 0; d < M; d++)
                        ans = max (ans, dp(i, j, 0, d));
                }
            }
        printf ("%d\n", ans);
    }
    return 0;
}

HDU5024Wang Xifeng's Little Plot(记忆化搜索)

时间: 2025-01-11 00:56:02

HDU5024Wang Xifeng's Little Plot(记忆化搜索)的相关文章

HDU 5024 (广州网络赛) Wang Xifeng&#39;s Little Plot 记忆化搜索+枚举

Problem Description <Dream of the Red Chamber>(also <The Story of the Stone>) is one of the Four Great Classical Novels of Chinese literature, and it is commonly regarded as the best one. This novel was created in Qing Dynasty, by Cao Xueqin.

HDU5024 Wang Xifeng&#39;s Little Plot 【记忆化搜索】

Wang Xifeng's Little Plot Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 319    Accepted Submission(s): 214 Problem Description <Dream of the Red Chamber>(also <The Story of the Stone>)

HDU 5024 Wang Xifeng&#39;s Little Plot (枚举 + DFS记忆化搜索)

Wang Xifeng's Little Plot Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 513    Accepted Submission(s): 338 Problem Description <Dream of the Red Chamber>(also <The Story of the Stone>)

hdu5024Wang Xifeng&#39;s Little Plot(思维|搜索)

题目链接: huangjing 题意: 从图中任何一个点走,最多只能转90度的弯,并且只能转一次弯,求这个最长路.. 思路: ym的朝鲜选手的代码,真厉害啊,它是每个点的8个方向都走一遍,然后最后遍历全图,去每个点最多转一次弯的最大长度,这个确实有点厉害,相当于枚举拐点..还有就是方向最好的按顺序来,因为一个直角正好隔两个方向的位,好运算.. 题目 Wang Xifeng's Little Plot Time Limit: 2000/1000 MS (Java/Others)    Memory

hdu5024-Wang Xifeng&#39;s Little Plot

此题一开始用暴力做,后来发现斜着走的时候其实暴力不太好写,于是改用搜索写了 1 #include <iostream> 2 #include <stdio.h> 3 #include <memory.h> 4 using namespace std; 5 6 char a[110][110]= {0}; 7 int down[110][110]= {0}; 8 int up[110][110]= {0}; 9 10 int cnt[110][110][4]= {0};

HDU 1513 Palindrome:LCS(最长公共子序列)or 记忆化搜索

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1513 题意: 给你一个字符串s,你可以在s中的任意位置添加任意字符,问你将s变成一个回文串最少需要添加字符的个数. 题解1(LCS): 很神奇的做法. 先求s和s的反串的LCS,也就是原串中已经满足回文性质的字符个数. 然后要变成回文串的话,只需要为剩下的每个落单的字符,相应地插入一个和它相同的字符即可. 所以答案是:s.size()-LCS(s,rev(s)) 另外,求LCS时只会用到lcs[i-

uva 1076 - Password Suspects(AC自动机+记忆化搜索)

题目链接:uva 1076 - Password Suspects 题目大意:有一个长度为n的密码,存在m个子串,问说有多少种字符串满足,如果满足个数不大于42,按照字典序输出. 解题思路:根据子串构建AC自动机,然后记忆化搜索,dp[i][u][s]表示第i个字符,在u节点,匹配s个子串. #include <cstdio> #include <cstring> #include <queue> #include <string> #include <

poj 1579(动态规划初探之记忆化搜索)

Function Run Fun Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 17843   Accepted: 9112 Description We all love recursion! Don't we? Consider a three-parameter recursive function w(a, b, c): if a <= 0 or b <= 0 or c <= 0, then w(a, b

记忆化搜索,FatMouse and Cheese

1.从gird[0][0]出发,每次的方向搜索一下,每次步数搜索一下 for(i=0; i<4; i++) { for(j=1; j<=k; j++) { int tx=x+d[i][0]*j; int ty=y+d[i][1]*j; if(tx>=0&&tx<n&&ty>=0&&ty<n&&grid[x][y]<grid[tx][ty]) { int temp=memSearch(tx,ty); i