题目链接:点击链接

题目大意:老鼠从(0,0)出发,每次在同一个方向上最多前进k步,且每次到达的位置上的数字都要比上一个位置上的数字大,求老鼠经过的位置上的数字的和的最大值

  1. #include<stdio.h>
  2. #include<string.h>
  3. #define max(a,b) a>b?a:b
  4. int n;
  5. int k;//前进的步数
  6. int map[105][105];
  7. int ans[105][105];//记忆化搜索,保存中间搜索结果
  8. int search(int x,int y)
  9. {
  10. int dx,dy;//要去的下一个位置
  11. int i,maxx = 0;
  12. if(ans[x][y] != -1) return ans[x][y];//已经搜索过,直接返回结果
  13. for(i = 1 ; i <= k ; i ++)//向前走的步数
  14. {
  15. dx = x - i;//向上
  16. if(dx >= 0 && dx < n && map[dx][y] > map[x][y])
  17. {
  18. ans[dx][y] = search(dx,y);
  19. maxx = max(maxx,ans[dx][y]);
  20. }
  21. dx = x + i;//向下
  22. if(dx >= 0 && dx < n && map[dx][y] > map[x][y])
  23. {
  24. ans[dx][y] = search(dx,y);
  25. maxx = max(maxx,ans[dx][y]);
  26. }
  27. dy = y - i;//向左
  28. if(dy >= 0 && dy < n && map[x][dy] > map[x][y])
  29. {
  30. ans[x][dy] = search(x,dy);
  31. maxx = max(maxx,ans[x][dy]);
  32. }
  33. dy = y + i;//向右
  34. if(dy >= 0 && dy < n && map[x][dy] > map[x][y])
  35. {
  36. ans[x][dy] = search(x,dy);
  37. maxx = max(maxx,ans[x][dy]);
  38. }
  39. }
  40. return maxx + map[x][y];
  41. }
  42. int main()
  43. {
  44. int i,j;
  45. while(scanf("%d%d",&n,&k) && (n != -1 && k != -1))
  46. {
  47. for(i = 0 ; i < n ; i ++)
  48. for(j = 0 ; j < n ; j ++)
  49. scanf("%d",&map[i][j]);
  50. memset(ans,-1,sizeof(ans));
  51. printf("%d\n",search(0,0));
  52. }
  53. return 0;
  54. }

zoj 1107 FatMouse and Cheese(记忆化搜索)的更多相关文章

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

    FatMouse has stored some cheese in a city. The city can be considered as a square grid of dimension ...

  2. P - FatMouse and Cheese 记忆化搜索

    FatMouse has stored some cheese in a city. The city can be considered as a square grid of dimension ...

  3. hdu1078 FatMouse and Cheese(记忆化搜索)

    转载请注明出处:http://blog.csdn.net/u012860063 题目链接:pid=1078" target="_blank">http://acm. ...

  4. HDU 1078 FatMouse and Cheese 记忆化搜索DP

    直接爆搜肯定超时,除非你加了某种凡人不能想出来的剪枝...555 因为老鼠的路径上的点满足是递增的,所以满足一定的拓补关系,可以利用动态规划求解 但是复杂的拓补关系无法简单的用循环实现,所以直接采取记 ...

  5. hdu1078 FatMouse and Cheese —— 记忆化搜索

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1078 代码1: #include<stdio.h>//hdu 1078 记忆化搜索 #in ...

  6. [HDOJ1078]FatMouse and Cheese(记忆化搜索)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1078 题意:给出n, k,然后给出n*n的地图,(下标0~n-1),有一只老鼠从(0,0)处出发,只能 ...

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

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1078 老鼠初始时在n*n的矩阵的(0 , 0)位置,每次可以向垂直或水平的一个方向移动1到k格,每次移 ...

  8. HDU 1078 FatMouse and Cheese (记忆化搜索+dp)

    详见代码 #include <iostream> #include <cstdio> #include <cstdlib> #include <memory. ...

  9. HDU ACM 1078 FatMouse and Cheese 记忆化+DFS

    题意:FatMouse在一个N*N方格上找吃的,每一个点(x,y)有一些吃的,FatMouse从(0,0)的出发去找吃的.每次最多走k步,他走过的位置能够吃掉吃的.保证吃的数量在0-100.规定他仅仅 ...

随机推荐

  1. redis(三)redis+Keepalived主从热备秒级切换

    一 简介 安装使用centos 5.10 Master 192.168.235.135 Slave 192.168.235.152 Vip 192.168.235.200 编译环境 yum -y in ...

  2. Google Map Android api V2 中使用MapView遇到CameraUpdateFactory is not initialized!的解决办法

    先说一下 Map V2 API Key 的问题吧: 在打包APP时需要自己生成一个XXX.keystore 用这个密室库生成的SHA1去申请的key 作为AndroidManifest.xml 中的K ...

  3. debian支持ll命令

    debian支持ll命令 $ ll -bash: ll: command not found 没有ll这个命令.尽管也知道ll事实上 是ls -l 这个命令的别名,可是总感觉不是非常习惯.由于之前一直 ...

  4. Adobe Acrobat Ⅺ Pro安装激活

    1.注意一定要断网安装,如果你有防火墙拦截亦可(注意:系统自带那防火墙不行). 2.将AcrobatPro_11_Web_WWMUI.exe解压到一个目录下,找到目录下的setup.exe安装,安装时 ...

  5. TimeUnit

    转http://blog.csdn.net/hudashi/article/details/6936604 public enum TimeUnitextends Enum<TimeUnit&g ...

  6. Swift - 异步加载各网站的favicon图标,并在单元格中显示

    下面是一个简单的应用,表格视图的各个单元格自动异步加载各个网站的favicon图标,并显示出来. 主要是复习下如何自定义单元格,单元格中图片的异步加载,以及didSet的用法. 效果图如下: 操作步骤 ...

  7. 【j2ee】div浮动层拖拽

    背景:近期项目中需要实现弹出浮层增加数据,并且浮动层可以拖拽 解决步骤:1.浮动层实现  2.拖拽实现 多方查资料,基本实现功能,现做demo,便于以后使用 先上图片大体展示实现效果: 再上代码,展示 ...

  8. PHP - 自定义函数

    第7章 自定义函数 学习要点: 1.标准函数 2.自定义函数 3.文件包含 4.魔法常量 一般来讲,冗余的代码都是不好的.一而再,再而三地重写代码不仅浪费时间,从布局结构角度看也显得粗制滥造.与所有优 ...

  9. Opera浏览器测试移动端网站和模拟手机浏览器的方法

    链接地址:http://www.neirong.org/post-256.html?utm_source=tuicool Chrome浏览器请看:Chrome浏览器测试移动端网站和模拟手机浏览器的方法 ...

  10. mac 压缩png图片资源 pngcrush命令

    iOS应用是,xcode会对PNG文件进行优化,这样图片预览就无法显示.那么如何查看这些经过优化的文件呢? 优化的动作是由pngcrush 工具完成的,你可以再xcode中找到.pngcrush 支持 ...