题目:

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.

InputThere are several test cases. Each test case consists of

a line containing two integers between 1 and 100: n and k 
n lines, each with n numbers: the first line contains the number of blocks of cheese at locations (0,0) (0,1) ... (0,n-1); the next line contains the number of blocks of cheese at locations (1,0), (1,1), ... (1,n-1), and so on. 
The input ends with a pair of -1's. 
OutputFor each test case output in a line the single integer giving the number of blocks of cheese collected. 
Sample Input

3 1
1 2 5
10 11 6
12 12 7
-1 -1

Sample Output

37

题意:

给出n*n矩阵,以及每一步能够走的步数,并且矩阵每个格子都有对应的数字表示奶酪数,求出老鼠能够吃到的最多的奶酪数,(前一个格子的奶酪数少于后一个格子的)

分析:

dp[i][j]表示在(i,j)点的时候的最大奶酪数,没说范围,那就dfs吧,好理解;

代码;

#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<math.h>
using namespace std;
int dp[111][111],a[111][111];
int neext[4][2]={0,1,0,-1,1,0,-1,0};
int n,k;
int judge(int x,int y)
{
    if(x<0||y<0||x>=n||y>=n)
        return 1;
    return 0;
}
int dfs(int x,int y)
{
    int i,j,xx,yy,ans=0;
    if(!dp[x][y])
    {
        for(i=1;i<=k;i++)是这吧每种能走的步数都试一下
            {
                for(j=0;j<4;j++)
                {
                    xx=x+neext[j][0]*i;
                    yy=y+neext[j][1]*i;
                    if(!judge(xx,yy)&&a[xx][yy]>a[x][y])
                        ans=max(ans,dfs(xx,yy));
                }
            }
            dp[x][y]=ans+a[x][y];
    }
    return dp[x][y];
}
int main()
{
    int i,j,m,l,res;
    while(scanf("%d %d",&n,&k)!=EOF)
    {
        if(n==-1&&k==-1)
            break;
        for(i=0;i<n;i++)
            for(j=0;j<n;j++)
                scanf("%d",&a[i][j]);
        memset(dp,0,sizeof(dp));
        res=dfs(0,0);
        printf("%d\n",res);
    }
    return 0;
}

ACM DP FatMouse and Cheese相关推荐

  1. HDU ACM 1078 FatMouse and Cheese 记忆化+DFS

    题意:FatMouse在一个N*N方格上找吃的,每一个点(x,y)有一些吃的,FatMouse从(0,0)的出发去找吃的.每次最多走k步,他走过的位置能够吃掉吃的.保证吃的数量在0-100.规定他仅仅 ...

  2. 动态规划训练21 [FatMouse and Cheese HDU - 1078 ]

    FatMouse and Cheese HDU - 1078 这道题需要说一说,定义dp[x][y]表示从点(x,y)出发,每次走不超过k步,所能吃到的最大量. 有点难搞的是,这里递归的顺序不好确定, ...

  3. HDUOJ 1078 FatMouse and Cheese

    HDUOJ 1078 FatMouse and Cheese 题目链接 Problem Description FatMouse has stored some cheese in a city. T ...

  4. zoj 1107 FatMouse and Cheese 逆向动态规划

    FatMouse and Cheese 开始试着用深度搜索做,超时了(对于时间复杂度没有概念) 这道题用动态规划.如果用自顶向下的方法,我们不知道结尾的是哪个点,所以不方便用. 如果我们采用自下而上的 ...

  5. 杭电OJ——ACM 1009.FatMouse‘ Trade

    FatMouse'Trade 杭电OJ--ACM 1009.FatMouse' Trade链接入口 问题描述        肥老鼠换东西,m磅猫食,n间房子,每个房间有J[i]磅JavaBean,对应 ...

  6. 【HDU - 1078】FatMouse and Cheese (记忆化搜索dp)

    题干: FatMouse has stored some cheese in a city. The city can be considered as a square grid of dimens ...

  7. FatMouse and Cheese HDU - 1078(记忆化搜索入门模板)

    题意: n * n的正方形格子(每个格子均放了奶酪),老鼠从(0,0)开始,每次最多移动k步,可以选择上下左右四个方向移动,下一个移动点奶酪块数量必须要大于当前点. 整理模板ing- 题目: FatM ...

  8. HDU - 1078 FatMouse and Cheese

    再谈记忆化搜索 题目描述: FatMouse has stored some cheese in a city. The city can be considered as a square grid ...

  9. 记忆化搜索,FatMouse and Cheese

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1107 http://acm.hdu.edu.cn/showpro ...

最新文章

  1. 强化学习常用算法+实际应用 ,必须get这些核心要点!
  2. python中的高阶函数
  3. scanf_s()函数 (是Microsoft公司VS开发工具提供的一个功能相同的安全标准输入函数)
  4. 程序员修炼之道-笔记
  5. 浏览器json格式化插件 yformater
  6. 浪潮云完成6亿元B轮融资,正推进上市;VMware收购AI初创公司Bitfusion;小爱同学App在苹果应用商店下架……...
  7. ADO.NET 命名规范
  8. 领域搜索算法 是什么 和遗传算法、模拟退火算法、禁忌搜索算法、模糊优化 算法、微粒群算法关系
  9. 个税倒推收入的计算器_手把手教你做个税计算器(1)
  10. 用WinRAR加密压缩文件
  11. QQ空间登录参数分析Firefox+Firebug
  12. ART加载OAT文件的过程分析
  13. pycharm: Error: Cannot run program……
  14. Java 生成数字证书系列(四)生成数字证书(续)
  15. Excel 去除重复行
  16. E. MEX and Increments---dp+优先队列+贪心
  17. 塑胶模具设计相关知识
  18. 计算机科学 在职双证,计算机专业在职研究生有双证的吗?
  19. linux sysinfo ()
  20. 英飞凌硅麦焊接注意事项

热门文章

  1. JavaScript常用技巧:stroage封装
  2. adobe air 通用设置
  3. ETHA Lend完成160万美元融资---为DeFi领域带来全新的收益优化协议
  4. spring实战学习(四)AOP及其实现方式
  5. 【3D数学】03 - 线性变换
  6. 深度好文 | YOLOv5+DeepSORT多目标跟踪深入解读与测试(含源码)
  7. CSS floats来创建三栏网页布局的方法
  8. 计算机 拔电源 重启,电脑在关机就自动重新启动。但拔掉电源插头再关就又不会重新启动了。求高手帮忙!!!...
  9. 爬取胸罩数据发现惊天秘密,D罩杯尽然。。。
  10. 2020身高体重标准表儿童_2020儿童身高体重标准表