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. Thinkphp3.2版本使用163邮箱发(验证码)邮件

    今天忽然想写一个用户修改密码的功能,又没有短信接口,只能选择用邮箱发送验证码啦,穷啊,没办法,哈哈,以下为正文. ------------------------------------------- ...

  2. css样式引入优先级?

    css中的优先级讲的有 1.选择器的优先级. 2.样式引入的优先级. 今天要研究的是样式引入的优先级.网上又很多答案都是如下的,但是真的是这样的吗,让我们来探一探究竟是如何. 四种样式的优先级别是:行 ...

  3. 最小覆盖_KEY

    最小覆盖(cover)..线段树 [题目描述] 给定 N 个区间[Li,Ri],需要你按照顺序选出一个区间序列使得[1,M]完全被覆盖.并且在选出来的序列中,某个区间[a,b]之前必须保证[1,a]都 ...

  4. RG_5

    必须发博纪念经过昨天的开车, 作业本终于做完啦!!! 可以认真的刷题了.

  5. Button标签自动刷新问题

    在form表单中,button标签在IE浏览器 type类型默认是button ,而在其他浏览器默认是submit. 解决方法1: 设置类型type="button" <bu ...

  6. WordPress-基础设置之常规设置

    对于第一次使用Wordpress系统的朋友,请先别着急发布文章及进行其他操作,为了更加科学的使用及管理wordpress,应该需要对其进行相关设置,主要涉及3个部分,一.常规设置,二.阅读设置,三.固 ...

  7. MySQL Base

    /* 连接与断开服务器 */ mysql -h 地址 -P 端口 -u 用户名 -p 密码 ---> input pwd /* 数据库存贮引擎 */    InnoDB :        1) ...

  8. 入门VMware Workstation下的Debian学习之Vim简单使用(三)

    什么是Vim? Vim具有程序编辑的能力,可以主动的以字体颜色辨别语法的正确性,方便程序设计. Vim是从vi发展出来的一个文本编辑器.代码补完.编译及错误跳转等方便编程的功能特别丰富,在程序员中被广 ...

  9. 【JAVA零基础入门系列】Day8 Java的控制流程

    什么是控制流程?简单来说就是控制程序运行逻辑的,因为程序一般而言不会直接一步运行到底,而是需要加上一些判断,一些循环等等.举个栗子,就好比你准备出门买个苹果,把这个过程当成程序的话,可能需要先判断一下 ...

  10. Vue的条件渲染

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...