码迷,mamicode.com
首页 > 其他好文 > 详细

HDU-1078-FatMouse and Cheese(记忆化搜索)

时间:2019-10-13 00:29:06      阅读:107      评论:0      收藏:0      [点我收藏+]

标签:names   mat   hdu   while   string   red   memory   思路   not   

链接:

https://vjudge.net/problem/HDU-1078#author=0

题意:

FatMouse has stored some cheese in a city. The city can be considered as a square grid of dimension n: each grid location is labelled (p,q) where 0 <= p < n and 0 <= q < n. At each grid location Fatmouse has hid between 0 and 100 blocks of cheese in a hole. Now he‘s going to enjoy his favorite food.

FatMouse begins by standing at location (0,0). He eats up the cheese where he stands and then runs either horizontally or vertically to another location. The problem is that there is a super Cat named Top Killer sitting near his hole, so each time he can run at most k locations to get into the hole before being caught by Top Killer. What is worse -- after eating up the cheese at one location, FatMouse gets fatter. So in order to gain enough energy for his next run, he has to run to a location which have more blocks of cheese than those that were at the current hole.

Given n, k, and the number of blocks of cheese at each grid location, compute the maximum amount of cheese FatMouse can eat before being unable to move.

思路:

Dp[i, j]记录某个位置的最大值, 对于跑过的点不用在搜索.
枚举所有能到的点,dfs.

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
//#include <memory.h>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <math.h>
#include <stack>
#include <string>
#include <assert.h>
#include <iomanip>
#include <iostream>
#include <sstream>
#define MINF 0x3f3f3f3f
using namespace std;
typedef long long LL;
const LL MOD = 20090717;
const int MAXN = 1e3+10;

int Map[MAXN][MAXN];
LL Dp[MAXN][MAXN];
int Next[4][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
int n, k;

LL Dfs(int x, int y)
{
    if (Dp[x][y] != -1)
        return Dp[x][y];
    LL val = Map[x][y];
    for (int i = 0;i < 4;i++)
    {
        for (int j = 1;j <= k;j++)
        {
            int tx = x+Next[i][0]*j;
            int ty = y+Next[i][1]*j;
            if (tx < 1 || tx > n || ty < 1 || ty > n || Map[tx][ty] <= Map[x][y])
                continue;
            val = max(val, Dfs(tx, ty)+Map[x][y]);
        }
    }
    Dp[x][y] = val;
    return Dp[x][y];
}

int main()
{
    while(~scanf("%d%d", &n, &k) && n != -1)
    {
        memset(Dp, -1, sizeof(Dp));
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= n; j++)
                scanf("%d", &Map[i][j]);
        }
        printf("%lld\n", Dfs(1, 1));
    }

    return 0;
}

HDU-1078-FatMouse and Cheese(记忆化搜索)

标签:names   mat   hdu   while   string   red   memory   思路   not   

原文地址:https://www.cnblogs.com/YDDDD/p/11664388.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!