zoukankan      html  css  js  c++  java
  • HDU-1078-FatMouse and Cheese(记忆化搜索)

    链接:

    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
    ", Dfs(1, 1));
        }
    
        return 0;
    }
    
  • 相关阅读:
    要给自己留时间思考
    联表更新 Update Left Join
    SQLServer2014内存优化表评测
    SQL Server中数据库文件的存放方式,文件和文件组
    {好文备份}SQL索引一步到位
    qt调用js,js调用qt
    【转】vs2010打开qt的.pro文件时错误解决办法
    qt多线程信号槽传输方式
    【转】设置Qt应用程序图标及应用程序名
    【转】Qt多线程操作界面---在QThread更新QProgressBar
  • 原文地址:https://www.cnblogs.com/YDDDD/p/11664388.html
Copyright © 2011-2022 走看看