hdu 1078 FatMouse and Cheese (dfs+记忆化搜索)
pid=1078">FatMouse and Cheese
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4811 Accepted Submission(s): 1945
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.
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.
3 1
1 2 5
10 11 6
12 12 7
-1 -1
37
题意是说每次能够走(1~K)个在同一直线的位置,即不能拐弯走。
数组较大,用记忆化搜索。
(类似于poj1088滑雪)
#include"stdio.h"
#include"string.h"
#include"queue"
#include"vector"
#include"stack"
#include"algorithm"
using namespace std;
#define N 105
#define max(a,b) (a>b?a:b)
int g[N][N],n,k;
int h[N][N];
int dir[4][2]={0,1,0,-1,-1,0,1,0};
int judge(int x,int y)
{
if(x<0||x>=n||y<0||y>=n)
return 0;
return 1;
}
int dfs(int x,int y)
{
if(h[x][y])
return h[x][y];
int i,j,u,v,t,sum=0,s1;
t=g[x][y];
for(i=0;i<4;i++)
{
for(j=1;j<=k;j++)
{
u=x+dir[i][0]*j;
v=y+dir[i][1]*j;
if(judge(u,v)&&g[u][v]>t)
{
s1=dfs(u,v);
sum=max(sum,s1);
}
}
}
return h[x][y]=g[x][y]+sum;
}
int main()
{
int i,j;
while(scanf("%d%d",&n,&k),n!=-1||k!=-1)
{
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&g[i][j]);
h[i][j]=0;
}
} int ans=dfs(0,0);
printf("%d\n",ans);
}
return 0;
}
hdu 1078 FatMouse and Cheese (dfs+记忆化搜索)的更多相关文章
- 随手练——HDU 1078 FatMouse and Cheese(记忆化搜索)
http://acm.hdu.edu.cn/showproblem.php?pid=1078 题意: 一张n*n的格子表格,每个格子里有个数,每次能够水平或竖直走k个格子,允许上下左右走,每次走的格子 ...
- HDU 1078 FatMouse and Cheese(记忆化搜索)
FatMouse and Cheese Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- HDU 1078 FatMouse and Cheese【记忆化搜索】
题意:给出n*n的二维矩阵,和k,老鼠每次最多走k步,问老鼠从起点(0,0)出发,能够得到的最大的数(即为将每走过一点的数都加起来的和最大)是多少 和上一题滑雪一样,搜索的方向再加一个循环 #incl ...
- HDU - 1078 FatMouse and Cheese(记忆化+dfs)
FatMouse and Cheese FatMouse has stored some cheese in a city. The city can be considered as a squar ...
- hdu1078 FatMouse and Cheese(记忆化搜索)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1078 题目大意: 题目中的k表示横向或者竖直最多可曾经进的距离,不可以拐弯.老鼠的出发点是(1,1) ...
- zoj 1107 FatMouse and Cheese(记忆化搜索)
题目链接:点击链接 题目大意:老鼠从(0,0)出发,每次在同一个方向上最多前进k步,且每次到达的位置上的数字都要比上一个位置上的数字大,求老鼠经过的位置上的数字的和的最大值 #include<s ...
- hdu 1078 FatMouse and Cheese(记忆搜)
N*N的矩阵,每个格子上有一个值. 老鼠起始在(1,1),每次只能水平着走或垂直着走.且最多只能走K步.且走到的格子里的值必须比上一次呆的格子里的值大. 问老鼠最多收集到多少值. 思路: 记忆搜好写. ...
- HDU 1078 FatMouse and Cheese ( DP, DFS)
HDU 1078 FatMouse and Cheese ( DP, DFS) 题目大意 给定一个 n * n 的矩阵, 矩阵的每个格子里都有一个值. 每次水平或垂直可以走 [1, k] 步, 从 ( ...
- 不要62 hdu 2089 dfs记忆化搜索
题目:http://acm.hdu.edu.cn/showproblem.php?pid=2089 题意: 给你两个数作为一个闭区间的端点,求出该区间中不包含数字4和62的数的个数 思路: 数位dp中 ...
- dfs+记忆化搜索,求任意两点之间的最长路径
C.Coolest Ski Route 题意:n个点,m条边组成的有向图,求任意两点之间的最长路径 dfs记忆化搜索 #include<iostream> #include<stri ...
随机推荐
- TCP经受时延的ACK
下午看<卷1>的时候,感觉“TCP经受时延ACK”这段看的有些迷糊,最后还算理解了,所以这里记下来以免以后又忘了. 经受时延的ACK就是在接收到数据后不立马确认,而是等到内核的一个定时器到 ...
- Chapter15:派生类
在C++语言中,基类将类型相关的函数与派生类不做改变直接继承的函数区别对待,对于某些函数,基类希望它的派生类各自定义适合自身的版本,此时基类就将这些函数声明为虚函数. 派生类必须将其继承而来的成员函数 ...
- 反汇编一个简单的C程序
一.实验截图 二.汇编代码分析: cpu首先执行main函数里的pushl %ebp和movl %esp %ebp.如下图: esp减去4就是向上移动4位到1,如下图: 把1赋值给esp,如下图: c ...
- Axure中继器基础教程(增行、删当前、标记、全部、规则行) Mark
---恢复内容开始--- 一.中继器的新增行 中继器所显示的列表项与中继器的数据集有关,默认情况下是一一对应的.也就是说,中继器数据集中有多少行数据,那么,列表项就有多少个. 那么,我们能不能通过新增 ...
- 我的新计划 《2Dof Racing Simulator》2014/3/9 20:30:00
最近好久都没来网站上了,也没心思和时间去弄VellLock和升级V&View了.一直在蕴量这做一件大玩意. 最近一直都很忙,忙着做数电课设,还有各种实验,可是我的心思不在这些东西上,当然除了数 ...
- Strider SSH Deploy配置
登录需要ssh, ssh 免密码登录配置自行百度.shell里写成自己的需要的命令
- shell脚本操作mysql库
shell脚本操作mysql数据库-e参数执行各种sql(指定到处编码--default-character-set=utf8 -s,去掉第一行的字段名称信息-N) 2011-05-11 18:18: ...
- Django 1.6 最佳实践: 如何正确使用 Signal(转)
原文:http://www.weiguda.com/blog/38/ 如何正确的使用signal: 简单回答是: 在其他方法无法使用的情况下, 才最后考虑使用signal. 因为新的django开发人 ...
- Hibernate理论
1.什么是Hibernate? Hibernate是数据持久层的一个轻量级框架.数据持久层的框架有很多比如:iBATIS,myBatis,Nhibernate,Siena等等.并且Hibernate是 ...
- hdu 1260 Tickets
http://acm.hdu.edu.cn/showproblem.php?pid=1260 题目大意:n个人买票,每个人买票都花费时间,相邻的两个人可以一起买票以节约时间: 所以一个人可以自己买票也 ...