FatMouse and Cheese (DFS + DP 记忆化搜索)
来源:互联网 发布:歼31和f35对比数据 编辑:程序博客网 时间:2024/06/06 04:37
FatMouse and Cheese
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 9643 Accepted Submission(s): 4067
Problem Description
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.
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
There 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.
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
For each test case output in a line the single integer giving the number of blocks of cheese collected.
Sample Input
3 11 2 510 11 612 12 7-1 -1
Sample Output
37
思路:记忆化搜索,类似数塔问题,找到以某个X,Y为起点向后搜索的最大值后,储存在dp数组中,下次直接用即可,大大减少了重复操作,值得注意的是,这题中走k步只能走直线,而题目中好像没有明确说明。
代码如下:
#include<cstdio>#include<cstring>#include<iostream>#include<cmath>using namespace std;int mp[105][105];int dp[105][105];int dir[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};int n, k;int dfs(int x, int y){if(dp[x][y] != -1){//如果已经找到已x,y为起点的最大值,则直接返回 return dp[x][y];}int tx, ty, maxx = 0;for(int i = 1; i <= k; i++){for(int j = 0; j < 4; j++){//往某个方向走k步 tx = x + i * dir[j][0];ty = y + i * dir[j][1];if(tx >= 0 && tx < n && ty >= 0 && ty < n && mp[tx][ty] > mp[x][y]){//如果满足这些条 maxx = max(maxx, dfs(tx, ty));}}}return dp[x][y] = maxx + mp[x][y];//返回已x,y为起点往后搜索的最大值,并且记录在dp数组中 }int main(){int i, j, ans;while(scanf("%d%d", &n, &k) != EOF){if(n == -1 && k == -1){break;}memset(vis, 0, sizeof(vis));//初始化 memset(dp, -1, sizeof(dp));for(i = 0; i < n; i++){for(j = 0; j < n; j++){scanf("%d", &mp[i][j]);}}cout<<dfs(0, 0)<<endl;}return 0;}
0 0
- FatMouse and Cheese (DFS + DP 记忆化搜索)
- hdu 1078 FatMouse and Cheese (dfs+记忆化搜索)
- HDU1078 FatMouse and Cheese(DFS记忆化搜索)
- HDU 1078 FatMouse and Cheese (dfs + dp记忆化搜索)
- hdu(1078) FatMouse and Cheese (记忆化搜索+dp)
- !HDU 1078 FatMouse and Cheese-dp-(记忆化搜索)
- HDU FatMouse and Cheese (记忆化搜索+dp思想)
- HDU1078:FatMouse and Cheese(记忆化 dp+搜索) (P)
- HDU1078 FatMouse and Cheese DP(记忆化搜索)
- hdu1078+FatMouse and Cheese+DFS+记忆化搜索
- HDU1078 FatMouse and Cheese [记忆化搜索DFS]
- 杭电1078 FatMouse and Cheese DFS 记忆化搜索
- hdu 1078 FatMouse and Cheese(dp 记忆化搜索)
- HDU 1078 FatMouse and Cheese 简单DP&记忆化搜索
- HDU 1078 FatMouse and Cheese 记忆化搜索模板 dp
- [HDU 1078 ] FatMouse and Cheese [ dp 记忆化搜索 ]
- hdu1078 FatMouse and Cheese 搜索dfs&dp
- hdu-1078 FatMouse and Cheese (and) 滑雪问题(记忆化搜索+简单dp)
- HDU 3371 Connect the Cities 【kruskal】
- Some file crunching failed, see logs for details解决办法
- 简单的划分数
- 55. Jump Game
- List Leaves
- FatMouse and Cheese (DFS + DP 记忆化搜索)
- 第二周作业
- Hibernate Criteria 使用
- 归并排序学习笔记——java封装类实现
- PHP中const和define()定义常量的细节区别
- Java 知识点 类,字符串
- 一个高级java开发工程师的基本修养
- python常用环境配置
- ToLua SimpleFramework NGUI/UGUI基础知识[2]