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
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std; #define MAXN 101
/*
可以向一个方向最多移动k步
因为从一个位置进入所获得的收益是固定的和前面的状态没有关系,所以可以把这个结果记录下来节省时间 记忆化搜索 递归
在上下左右四个方向找 受益最大的格子同时记录
*/
int n,k,g[MAXN][MAXN],dp[MAXN][MAXN];
int dir[][] = {{,},{-,},{,},{,-}};
int Mdfs(int x,int y)
{
if(dp[x][y])
return dp[x][y];
int Max = ;
for(int i=;i<;i++)
{
for(int d=;d<=k;d++)
{
int nx = x + dir[i][]*d;
int ny = y + dir[i][]*d;
if(nx<n&&nx>=&&ny>=&&ny<n&&g[nx][ny]>g[x][y])
Max = max(Max,Mdfs(nx,ny));
}
}
dp[x][y] = Max + g[x][y];
return dp[x][y];
}
int main()
{
while(scanf("%d%d",&n,&k))
{
if(n==-&&k==-) break;
memset(dp,,sizeof(dp));
for(int i=;i<n;i++)
{
for(int j=;j<n;j++)
scanf("%d",&g[i][j]);
}
printf("%d\n",Mdfs(,));
}
return ;
}

P - FatMouse and Cheese 记忆化搜索的更多相关文章

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

    FatMouse has stored some cheese in a city. The city can be considered as a square grid of dimension ...

  2. hdu1078 FatMouse and Cheese(记忆化搜索)

    转载请注明出处:http://blog.csdn.net/u012860063 题目链接:pid=1078" target="_blank">http://acm. ...

  3. HDU 1078 FatMouse and Cheese 记忆化搜索DP

    直接爆搜肯定超时,除非你加了某种凡人不能想出来的剪枝...555 因为老鼠的路径上的点满足是递增的,所以满足一定的拓补关系,可以利用动态规划求解 但是复杂的拓补关系无法简单的用循环实现,所以直接采取记 ...

  4. hdu1078 FatMouse and Cheese —— 记忆化搜索

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1078 代码1: #include<stdio.h>//hdu 1078 记忆化搜索 #in ...

  5. [HDOJ1078]FatMouse and Cheese(记忆化搜索)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1078 题意:给出n, k,然后给出n*n的地图,(下标0~n-1),有一只老鼠从(0,0)处出发,只能 ...

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

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1078 老鼠初始时在n*n的矩阵的(0 , 0)位置,每次可以向垂直或水平的一个方向移动1到k格,每次移 ...

  7. HDU 1078 FatMouse and Cheese (记忆化搜索+dp)

    详见代码 #include <iostream> #include <cstdio> #include <cstdlib> #include <memory. ...

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

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

  9. !HDU 1078 FatMouse and Cheese-dp-(记忆化搜索)

    题意:有一个n*n的格子.每一个格子里有不同数量的食物,老鼠从(0,0)開始走.每次下一步仅仅能走到比当前格子食物多的格子.有水平和垂直四个方向,每一步最多走k格,求老鼠能吃到的最多的食物. 分析: ...

随机推荐

  1. log4net 简易封装

    using log4net; using log4net.Appender; using log4net.Config; using log4net.Core; using log4net.Layou ...

  2. 小白的python之路 序

    计算机专科毕业,.net开发已有8年有余,中途断断续续,似懂非懂,积累了一些经验知识,但是不求甚解,属于那种一瓶不满半瓶子晃荡,这么一个状态. 主要从事web开发,涉及一些前端jq等,还有接口开发,搜 ...

  3. NHibernate3.2学习笔记-几种查询方式

    一.开发环境 数据库:SQLServer2008 编译器:VS2010 .Net版本:.Net Framework 4.0 二.开发过程 1.项目结构 承接上一篇 2.执行sql语句 (1)基本语法 ...

  4. 网上商城 Incorrect datetime value: '' for column 'ordertime' at row 1

    今天在做商城项目的[提交订单]功能的时候,向数据库插入数据报错:Incorrect datetime value: '' for column 'ordertime' at row 1 public ...

  5. 6.12---select

  6. 关于java的print()

    print方法是类PrintStream的方法成员,而System类有一个static的PrintStream类型的属性成员,名叫out,我们平时写的System.out.print("he ...

  7. leetcode126 Word Ladder II

    思路: 宽搜过程中分层记录路径,递归还原.实现: class Solution { public: void getPath(string now, string beginWord, string ...

  8. VS2015 安装包缺失(联网安装失败)问题解决

    Win7 x86 测试可行 *  如果前面有尝试过安装不成功, 一定要用卸载程序删除已安装的部分,否则会出乱子. 1. 或者是用虚拟光驱加载ISO, 或者是解压到硬盘上, 都没有关系. 2. 用管理员 ...

  9. HTML `capture` 属性

    file 类型的 <input> 除了调起系统的文件选择框外,还可通过指定 capture 属性来现场拍照或录制.配合 accept 属性,可实现更加便捷的文件获取. 比如想要录制一段视频 ...

  10. 09Java Server Pages 错误处理

    Java Server Pages 错误处理 通常JSP在执行的时候,在两个阶段会发生错误.第一个是JSP网页转译成Servlet类的时候,另一个就是Servlet类处理每一个请求的时候.在第一个阶段 ...