P - FatMouse and Cheese 记忆化搜索
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 记忆化搜索的更多相关文章
- HDU - 1078 FatMouse and Cheese (记忆化搜索)
FatMouse has stored some cheese in a city. The city can be considered as a square grid of dimension ...
- hdu1078 FatMouse and Cheese(记忆化搜索)
转载请注明出处:http://blog.csdn.net/u012860063 题目链接:pid=1078" target="_blank">http://acm. ...
- HDU 1078 FatMouse and Cheese 记忆化搜索DP
直接爆搜肯定超时,除非你加了某种凡人不能想出来的剪枝...555 因为老鼠的路径上的点满足是递增的,所以满足一定的拓补关系,可以利用动态规划求解 但是复杂的拓补关系无法简单的用循环实现,所以直接采取记 ...
- hdu1078 FatMouse and Cheese —— 记忆化搜索
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1078 代码1: #include<stdio.h>//hdu 1078 记忆化搜索 #in ...
- [HDOJ1078]FatMouse and Cheese(记忆化搜索)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1078 题意:给出n, k,然后给出n*n的地图,(下标0~n-1),有一只老鼠从(0,0)处出发,只能 ...
- HDU 1078 FatMouse and Cheese (记忆化搜索)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1078 老鼠初始时在n*n的矩阵的(0 , 0)位置,每次可以向垂直或水平的一个方向移动1到k格,每次移 ...
- HDU 1078 FatMouse and Cheese (记忆化搜索+dp)
详见代码 #include <iostream> #include <cstdio> #include <cstdlib> #include <memory. ...
- HDU ACM 1078 FatMouse and Cheese 记忆化+DFS
题意:FatMouse在一个N*N方格上找吃的,每一个点(x,y)有一些吃的,FatMouse从(0,0)的出发去找吃的.每次最多走k步,他走过的位置能够吃掉吃的.保证吃的数量在0-100.规定他仅仅 ...
- !HDU 1078 FatMouse and Cheese-dp-(记忆化搜索)
题意:有一个n*n的格子.每一个格子里有不同数量的食物,老鼠从(0,0)開始走.每次下一步仅仅能走到比当前格子食物多的格子.有水平和垂直四个方向,每一步最多走k格,求老鼠能吃到的最多的食物. 分析: ...
随机推荐
- mycat登录报错Host 'XXX' is blocked because of many connection errors的另一种解决思路
报错时机 使用了mycat,而不是单纯使用了mysql. 报错信息 ERROR 1129 (HY000): Host '1.23.22.18' is blocked because of many c ...
- nat模式下更改网络环境, 虚拟机中Linux无法上网的问题
出现的问题: 1.ifconfig -a 命令下会出现eth0信息中无ip地址等等信息: 2.无法ping通baidu,也就是无法上网: 3.ping 8.8.8.8 提示 connect:netwo ...
- EditText(7)EditText输入事件监听
EditText.addTextChangedListener(TextWatcher watcher); void initSearch(){ search = (EditText) findVie ...
- 14 C#编程中的逻辑运算
在C#编程中,我们经常需要处理这些情况. 1. 某种条件为真时,程序这样处理:当某种条件为假时,程序那样处理. 2. 当某种条件为真时,程序一直这样处理: 这里的条件,在C#中就是逻辑运算.接下来我就 ...
- oracle创建临时表空间、用户表空间、创建用户关联表空间、授权等
1.创建临时表空间 CREATE TEMPORARY TABLESPACE test_temp TEMPFILE 'C:\oracle\product\10.1.0\oradata\orcl\test ...
- TF实战:(Mask R-CNN原理介绍与代码实现)-Chapter-8
二值掩膜输出依据种类预测分支(Faster R-CNN部分)预测结果:当前RoI的物体种类为i第i个二值掩膜输出就是该RoI的损失Lmask 对于预测的二值掩膜输出,我们对每个像素点应用sigmoid ...
- 文件和打印共享资源(IP地址)处于联机状态,但未对连接尝试做出响应。
文件和打印共享资源(IP地址)处于联机状态,但未对连接尝试做出响应. 检测到 远程计算机不接受端口 445 上的连接,这可能是由于防火墙或安全策略设置,或因为服务可能暂时不可用.Windows 在计算 ...
- JavaScipt30(第十八个案例)(主要知识点:Array.prototype.map)
承接上文,这是第十八个案例,中间的十到十八我直接看了答案,因为有些例子从他打开的页面看不出他要做什么. 附上项目链接: https://github.com/wesbos/JavaScript30 这 ...
- Linux下查看CPU信息、机器型号等硬件信息命令
Linux下查看CPU信息.机器型号等硬件信息命令 编写一个bash脚本: vim info.sh #!/bin/bash cat /etc/issue echo "____________ ...
- SpringMVC-Mybatis整合和注解开发
SpringMVC-Mybatis整合和注解开发SpringMVC-Mybatis整合整合的思路在mybatis和spring整合的基础上 添加springmvc.spring要管理springmvc ...