FatMouse and Cheese

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Submit Status

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.

 

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. 

 

Output

For 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
 
  1. /*
  2. 题意:给你一个n*n的矩阵,每个点都有一定数量的食物,出发点在(0,0),每次可以走最多k步,下一个单元格的食物数量
  3. 一定要比当前单元格的数量多,问你最多能吃到的食物数量
  4.  
  5. 初步思路:记忆化搜索
  6. */
  7. #include <bits/stdc++.h>
  8. #define N 110
  9. using namespace std;
  10. int n,k;
  11. int mapn[N][N];
  12. int dp[N][N];//dp[i][j]用于保存到达i j之后能吃到的最大食物数量
  13. int dir[][]={{,},{-,},{,},{,-}};
  14. bool vis[N][N];
  15. bool ok(int x,int y,int val){
  16. if(x<||x>=n||y<||y>=n||vis[x][y]||mapn[x][y]<=val) return false;
  17. return true;
  18. }
  19. int dfs(int x,int y){//三种 状态坐标,第几步
  20. if(dp[x][y]!=-) //遇到合适的状态直接返回就行了
  21. return dp[x][y];
  22. int cur=;
  23. for(int i=;i<;i++){
  24. int fx=x,fy=y;
  25. for(int j=;j<=k;j++){
  26. fx+=dir[i][];
  27. fy+=dir[i][];
  28. if(ok(fx,fy,mapn[x][y])==false) continue;
  29.  
  30. vis[fx][fy]=true;
  31.  
  32. cur=max(cur,dfs(fx,fy));
  33.  
  34. vis[fx][fy]=false; //一条路走完了,将标记清理干净
  35. }
  36. }
  37. return dp[x][y]=cur+mapn[x][y];
  38. }
  39. void init(){
  40. memset(dp,-,sizeof dp);
  41. memset(vis,false,sizeof vis);
  42. }
  43. int main(){
  44. // freopen("in.txt","r",stdin);
  45. while(scanf("%d%d",&n,&k)!=EOF&&(n!=-&&k!=-)){
  46. init();
  47. for(int i=;i<n;i++){
  48. for(int j=;j<n;j++){
  49. scanf("%d",&mapn[i][j]);
  50. }
  51. }//处理输入
  52. dfs(,);
  53. // for(int i=0;i<n;i++){
  54. // for(int j=0;j<n;j++){
  55. // cout<<dp[i][j]<<" ";
  56. // }
  57. // cout<<endl;
  58. // }
  59. printf("%d\n",dp[][]);
  60. }
  61. return ;
  62. }

FatMouse and Cheese的更多相关文章

  1. HDU 1078 FatMouse and Cheese(记忆化搜索)

    FatMouse and Cheese Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  2. hdu 1078 FatMouse and Cheese (dfs+记忆化搜索)

    pid=1078">FatMouse and Cheese Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/ ...

  3. FatMouse and Cheese 动态化搜索

    FatMouse and Cheese Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  4. (记忆化搜索) FatMouse and Cheese(hdu 1078)

    题目大意:   给n*n地图,老鼠初始位置在(0,0),它每次行走要么横着走要么竖着走,每次最多可以走出k个单位长度,且落脚点的权值必须比上一个落脚点的权值大,求最终可以获得的最大权值   (题目很容 ...

  5. HDU1078 FatMouse and Cheese(DFS+DP) 2016-07-24 14:05 70人阅读 评论(0) 收藏

    FatMouse and Cheese Problem Description FatMouse has stored some cheese in a city. The city can be c ...

  6. HDU 1078 FatMouse and Cheese ( DP, DFS)

    HDU 1078 FatMouse and Cheese ( DP, DFS) 题目大意 给定一个 n * n 的矩阵, 矩阵的每个格子里都有一个值. 每次水平或垂直可以走 [1, k] 步, 从 ( ...

  7. kuangbin专题十二 HDU1078 FatMouse and Cheese )(dp + dfs 记忆化搜索)

    FatMouse and Cheese Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  8. 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 ...

  9. HDU1078 FatMouse and Cheese 【内存搜索】

    FatMouse and Cheese Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

随机推荐

  1. 用JS制作一个信息管理平台完整版

      前  言 JRedu 在之前的文章中,介绍了如何用JS制作一个实用的信息管理平台. 但是那样的平台功能过于简陋了,我们今天来继续完善一下. 首先我们回顾一下之前的内容.   1.JSON的基础知识 ...

  2. angular-bootstrap ui-date组件问题总结

    使用angular框架的时候,之前用的时间控件是引入My97DatePicker组件实现的,但是因为 1.My97DatePicker样式不太好看以及偶尔会出现底部被遮盖的情况.点击不可编辑input ...

  3. struts1.3学习

    一.基本配置 参考博客 项目结构 web.xml <!-- struts配置 --> <servlet> <servlet-name>action</serv ...

  4. 新旧apache HttpClient 获取httpClient方法

    在apache httpclient 4.3版本中对很多旧的类进行了deprecated标注,通常比较常用的就是下面两个类了. DefaultHttpClient -> CloseableHtt ...

  5. ioc(Inversion of Control)控制反转和DI

    ioc意味着将你设计好的交给容器控制,而不是传统在你的对象中直接控制 谁控制了谁:传统的javaSE程序设计,我们直接在对象内部通过new进行创建对象,是程序主动去创建依赖对象:而ioc是有专门一个容 ...

  6. [Node.js] 2、利用node-git-server快速搭建git服务器

    本文用到了node-git-server 1.检测本地git版本 该包的使用需要机器上本来就安装git,且git的版本大于等于2.7: ╭─root@lt /home/workspace ╰─# gi ...

  7. python 保存文本txt格式之总结篇,ANSI,unicode,UTF-8

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAA4wAAAEmCAIAAACmsIlUAAAgAElEQVR4nOydezxU+f/HP49WSstKkZ

  8. The Nerd Factor SPOJ - MYQ5

    The Nerd Factor Time Limit: 1000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu [Submi ...

  9. bzoj2257 [Jsoi2009]瓶子和燃料 最大公约数

    [Jsoi2009]瓶子和燃料 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1449  Solved: 889[Submit][Status][Di ...

  10. 怎样让PDM图形列表显示name和code等需要的信息

    1. 工具(TOOLS)-〉显示参数设置(DISPLAY PREFERENCES) 2. 在弹出来的框中选中Content-〉Table 3. 点右下角那个Advanced 按钮 4. 在弹出的框个选 ...