HDU1078 FatMouse and Cheese(DFS+DP) 2016-07-24 14:05 70人阅读 评论(0) 收藏
FatMouse and Cheese
Problem Description
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.
Input
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.
Output
Sample Input
- 3 1
- 1 2 5
- 10 11 6
- 12 12 7
- -1 -1
Sample Output
- 37
——————————————————————————————————————————————————————
- #include <iostream>
- #include <cstring>
- using namespace std;
- int dir[4][2] = { { -1, 0 }, { 1, 0 }, { 0, -1 }, { 0, 1 } };
- int mp[105][105];
- int dp[105][105];
- int m, k;
- bool cheak(int i, int j)
- {
- if (i < 0 || i >= m || j < 0 || j >= m)
- return 0;
- else
- return 1;
- }
- int dfs(int x, int y)
- {
- if (dp[x][y] > 0)
- return dp[x][y];
- int mx = 0;
- int xx, yy;
- for (int i = 0; i < 4;i++)
- for (int j = 1; j <= k; j++)
- {
- xx = x + dir[i][0] * j;
- yy = y + dir[i][1] * j;
- if (cheak(xx, yy) && mp[xx][yy]>mp[x][y])
- {
- int t = dfs(xx, yy);
- if (t>mx)
- mx = t;
- }
- }
- dp[x][y] = mp[x][y] + mx;
- return dp[x][y];
- }
- int main()
- {
- while (scanf("%d%d", &m, &k) && (m != -1 || k != -1))
- {
- for (int i = 0; i < m;i++)
- for (int j = 0; j < m; j++)
- {
- scanf("%d", &mp[i][j]);
- }
- memset(dp, 0, sizeof(dp));
- int ans=dfs(0, 0);
- printf("%d\n", ans);
- }
- return 0;
- }
HDU1078 FatMouse and Cheese(DFS+DP) 2016-07-24 14:05 70人阅读 评论(0) 收藏的更多相关文章
- A Knight's Journey 分类: dfs 2015-05-03 14:51 23人阅读 评论(0) 收藏
A Knight’s Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 34085 Accepted: 11621 ...
- Hdu1016 Prime Ring Problem(DFS) 2016-05-06 14:27 329人阅读 评论(0) 收藏
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- HDU1426 Sudoku Killer(DFS暴力) 2016-07-24 14:56 65人阅读 评论(0) 收藏
Sudoku Killer Problem Description 自从2006年3月10日至11日的首届数独世界锦标赛以后,数独这项游戏越来越受到人们的喜爱和重视. 据说,在2008北京奥运会上,会 ...
- HDU1258 Sum It Up(DFS) 2016-07-24 14:32 57人阅读 评论(0) 收藏
Sum It Up Problem Description Given a specified total t and a list of n integers, find all distinct ...
- leetcode N-Queens/N-Queens II, backtracking, hdu 2553 count N-Queens, dfs 分类: leetcode hdoj 2015-07-09 02:07 102人阅读 评论(0) 收藏
for the backtracking part, thanks to the video of stanford cs106b lecture 10 by Julie Zelenski for t ...
- Hdu 1009 FatMouse' Trade 分类: Translation Mode 2014-08-04 14:07 74人阅读 评论(0) 收藏
FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- hdu 1231, dp ,maximum consecutive sum of integers, find the boundaries, possibly all negative, C++ 分类: hdoj 2015-07-12 03:24 87人阅读 评论(0) 收藏
the algorithm of three version below is essentially the same, namely, Kadane's algorithm, which is o ...
- A Knight's Journey 分类: POJ 搜索 2015-08-08 07:32 2人阅读 评论(0) 收藏
A Knight's Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 35564 Accepted: 12119 ...
- HDU1506(单调栈或者DP) 分类: 数据结构 2015-07-07 23:23 2人阅读 评论(0) 收藏
Largest Rectangle in a Histogram Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 ...
随机推荐
- SQL Server - 最佳实践 - 参数嗅探问题 转。
文章来自:https://yq.aliyun.com/articles/61767 先说我的问题,最近某个存储过程,暂定名字:sp_a 总是执行超时,sp_a带有一个参数,暂定名为 para1 var ...
- jsp 不显示换行 Eclipse复制一行快捷键
jsp通过out.println();不能换行.html中需要标签<br/>才可以 Eclipse 复制整行代码移动:Ctrl+Alt+↑/↓.但是跟系统屏幕上下切换冲突,可以选择,在电脑 ...
- redis的五种常见数据类型的常用指令
一.String字符串,key-value 应用场景:string是redis的最基本数据类型,key-value格式,一个key对应一个值的情况下 1.设置key = value:set key ...
- luoguP3367 [模板]并查集
题目链接:https://www.luogu.org/problemnew/show/P3367 思路: 今天学了新算法——并查集,本题是简单的并查集题的模板. 核心思想是“递归+压缩路径”. 并查集 ...
- OC 线程操作3 - NSOperation 实现线程间通信
#import "ViewController.h" @interface ViewController () /** 图片 */ @property (weak, nonatom ...
- discuz回贴通知插件实现-插件后台管理配置
1.登出discuz后台,再次设计插件 2.使用变量
- linux-redhat-git源码安装
1.查看是否已安装git,如果存在自带的git,则卸载 查看git版本 $ git --version 删除自带git $ yum remove git 2.安装依赖包 $ yum -y instal ...
- inux中Vi不能高亮显示行号的解决办法
适用版本:CentOS,RedHat,UBUNTU,Fedora解决办法如下: 在UBUNTU中vim的配置文件存放在/etc/vim目录中,配置文件名为vimrc 在Fedora中vim的配置文件存 ...
- BZOJ1222 [HNOI2001]产品加工 - 动态规划- 背包
题解 怎么看都不像是个背包,直到我看了题解→_→, 第一次碰到这么奇怪的背包= = 定一个滚动数组$F_i$, $i$表示机器$a$用了$i$的时间, $F_i$表示机器$b$用了$F_i$的时间, ...
- nzhtl1477-ただいま帰りました ( bfs )
nzhtl1477-ただいま帰りました 题目描述 珂学题意: 你是威廉!你要做黄油蛋糕给珂朵莉吃~! 68号岛有n个商店,有的商店直接有小路连接,小路的长度都为1 格里克告诉了你哪些地方可能有做黄油蛋 ...