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;
    }
    
  • 相关阅读:
    浅谈SQLite——查询处理及优化
    .NET 并行(多核)编程系列之七 共享数据问题和解决概述
    sql 存储过程学习一
    SQL中获得EXEC后面的sql语句或者存储过程的返回值的方法 【收藏】
    script刷新页面,刷新代码
    C#编程中关于数据缓存的经验总结
    SQL存储过程的概念,优点及语法
    SQLite数据库安装、试用及编程测试手记
    c# sqlite 数据库加密
    进销存管理系统的设计与实现
  • 原文地址:https://www.cnblogs.com/YDDDD/p/11664388.html
Copyright © 2011-2022 走看看