hdu 1078 FatMouse and Cheese (深度优先搜索+dp)
来源:互联网 发布:java摆脱内存泄露 编辑:程序博客网 时间:2024/06/08 17:15
FatMouse and Cheese
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 11156 Accepted Submission(s): 4731
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
/**老鼠走直线 距离《= k逆推 从 以 1 1 为起点不断地深度优先搜索下去**/#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;int m[103][103];int vis[103][103];int dx[4]= {0,0,-1,1};int dy[4]= {1,-1,0,0};int n,k;bool check(int i,int j){ if(i<=n&&i>=1&&j<=n&&j>=1) return true; return false;}int dfs(int x,int y){ int ans=0; if(!vis[x][y]) { for(int i=1; i<=k; i++) { int tx,ty; for(int j=0; j<4; j++) { tx=x+dx[j]*i; ty=y+dy[j]*i; if(!check(tx,ty))continue; if(m[x][y]<m[tx][ty]) ans=max(ans,dfs(tx,ty)); } } vis[x][y]=ans+m[x][y]; } return vis[x][y];}int main(){ while(scanf("%d%d",&n,&k)==2&&(n!=-1&&k!=-1)) { for(int i=1; i<=n; i++) { for(int j=1; j<=n; j++) { scanf("%d",&m[i][j]); } } memset(vis,0,sizeof(vis)); printf("%d\n",dfs(1,1)); }}
阅读全文
0 0
- hdu 1078 FatMouse and Cheese (深度优先搜索+dp)
- hdu 1078 FatMouse and Cheese (搜索+dp)
- hdu(1078) FatMouse and Cheese (记忆化搜索+dp)
- !HDU 1078 FatMouse and Cheese-dp-(记忆化搜索)
- hdu 1078 FatMouse and Cheese(dp 记忆化搜索)
- HDU 1078 FatMouse and Cheese 简单DP&记忆化搜索
- HDU 1078 FatMouse and Cheese 记忆化搜索模板 dp
- [HDU 1078 ] FatMouse and Cheese [ dp 记忆化搜索 ]
- HDU FatMouse and Cheese (记忆化搜索+dp思想)
- hdu-1078 FatMouse and Cheese (and) 滑雪问题(记忆化搜索+简单dp)
- hdu-1078 FatMouse and Cheese (and) 滑雪问题(记忆化搜索+简单dp)
- FatMouse and Cheese(HDU 1078) —— 记忆化搜索DP
- HDU 1078 FatMouse and Cheese(记忆化搜索 Or DP)
- HDU 1078 FatMouse and Cheese (dfs + dp记忆化搜索)
- hdu 1078(dfs+dp)FatMouse and Cheese
- HDU 1078 FatMouse and Cheese(DP)
- HDU 1078 FatMouse and Cheese(DP)
- HDU 1078 FatMouse and Cheese(经典DP)
- Git-SourceTree使用规范
- SQL Server中,备份数据表
- 一名合格的前端工程师的知识构成!
- 双栈排序
- PHP处理高精度运算
- hdu 1078 FatMouse and Cheese (深度优先搜索+dp)
- DOM节点层次(Element类型 Text类型)
- 关于PopupWindow一些用法(二)
- Android单元测试那些事儿(三)
- CSU 1216 异或最大值 01trie
- ES6新特性
- 四层、七层负载均衡的区别
- 设计4个线程,其中两个线程每次对j增加1,另外连个每次对j减少1,循环100次。
- select2 ajax 设置默认值,初始值