题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1078

题意:给出n, k,然后给出n*n的地图,(下标0~n-1),有一只老鼠从(0,0)处出发,只能走直线,并且目标点的数值比当前点要大。每次最长可以走k步,问最长的一条链的数值和。

用一个二维数组dp(i,j)表示某一格出发的时候最长链的数值和,然后dfs。

  1. #include <algorithm>
  2. #include <iostream>
  3. #include <iomanip>
  4. #include <cstring>
  5. #include <climits>
  6. #include <complex>
  7. #include <fstream>
  8. #include <cassert>
  9. #include <cstdio>
  10. #include <bitset>
  11. #include <vector>
  12. #include <deque>
  13. #include <queue>
  14. #include <stack>
  15. #include <ctime>
  16. #include <set>
  17. #include <map>
  18. #include <cmath>
  19.  
  20. using namespace std;
  21.  
  22. const int maxn = ;
  23. const int dx[] = {, , , -};
  24. const int dy[] = {, -, , };
  25. int n, k;
  26. int G[maxn][maxn];
  27. int dp[maxn][maxn];
  28.  
  29. bool ok(int x, int y) {
  30. return x >= && y >= && x < n && y < n;
  31. }
  32.  
  33. int dfs(int x, int y) {
  34. if(dp[x][y]) return dp[x][y];
  35. int cur = ;
  36. for(int i = ; i < ; i++) {
  37. for(int j = ; j <= k; j++) {
  38. int xx = x + dx[i] * j;
  39. int yy = y + dy[i] * j;
  40. if(ok(xx, yy) && G[xx][yy] > G[x][y]) {
  41. cur = max(cur, dfs(xx, yy));
  42. }
  43. }
  44. }
  45. return dp[x][y] = cur + G[x][y];
  46. }
  47.  
  48. inline bool scan_d(int &num) {
  49. char in;bool IsN=false;
  50. in=getchar();
  51. if(in==EOF) return false;
  52. while(in!='-'&&(in<''||in>'')) in=getchar();
  53. if(in=='-'){ IsN=true;num=;}
  54. else num=in-'';
  55. while(in=getchar(),in>=''&&in<=''){
  56. num*=,num+=in-'';
  57. }
  58. if(IsN) num=-num;
  59. return true;
  60. }
  61.  
  62. int main() {
  63. // freopen("in", "r", stdin);
  64. while(~scanf("%d%d", &n, &k) && n + k != -) {
  65. memset(dp, , sizeof(dp));
  66. for(int i = ; i < n; i++) {
  67. for(int j = ; j < n; j++) {
  68. scan_d(G[i][j]);
  69. }
  70. }
  71. printf("%d\n", dfs(, ));
  72. }
  73. return ;
  74. }

[HDOJ1078]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. HDU 1078 FatMouse and Cheese (记忆化搜索)

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

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

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

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

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

  9. !HDU 1078 FatMouse and Cheese-dp-(记忆化搜索)

    题意:有一个n*n的格子.每一个格子里有不同数量的食物,老鼠从(0,0)開始走.每次下一步仅仅能走到比当前格子食物多的格子.有水平和垂直四个方向,每一步最多走k格,求老鼠能吃到的最多的食物. 分析: ...

随机推荐

  1. 笔直的水管 usaco 背包

    背包dp入门,需要滚动数组: #include<iostream> #include<cstdio> #include<string> #include<cs ...

  2. html5 webApp常用Meta标签

    Html5 webApp常用Meta标签 <meta charset="UTF-8"> <meta name="viewport" conte ...

  3. PowerDesigner(五)-概念数据模型(CDM生成LDM,PDM和OOM)(转)

    概念数据模型 概念数据模型(Conceptual Data Model,CDM):表达的是数据整体逻辑结构,该结构独立于任何软件和数据存储结构,即它只是系统分析人员,应用程序设计人员,维护人员和用户之 ...

  4. IDA 在string窗口中显示中文字符串

    打开ida61\cfg中的ida.cfg文件找到 // (cp866 version)AsciiStringChars = "\r\n\a\v\b\t\x1B" " !\ ...

  5. Unity上使用Linq To XML

    using UnityEngine; using System.Collections; using System.Linq; using System.Xml.Linq; using System; ...

  6. 容器字段FieldContainer

    //Ext.form.FieldContainer扩展自Ext.container.Container.当需要把多个字段或组件作为一个表单项展示的时候就需要此组件          Ext.Quick ...

  7. iOS16进制设置颜色

    UIColor+Hex.h // // UIColor+Hex.h // 16进制颜色类别 // // Created by apple on 15-4-3. // Copyright (c) 201 ...

  8. 李洪强漫谈iOS开发[C语言-004]-开发概述程序设计语言程序编译过程

    汇编语言 指令用特定的名字来标记,这就是汇编语言 人比较容易看懂汇编语言 汇编直接和程序一一对应的 有汇编器把程序翻译成机器码 把高级语言编译成计算机识别的语言 程序编译过程 命令行 UNIX 系统中 ...

  9. Map中如何把没有定义操作符<的类作为key

    Map中如何把没有定义操作符<的类作为key 其实,为了实现快速查找,map内部本身就是按序存储的(比如红黑树).在我们插入<key, value>键值对时,就会按照key的大小顺序 ...

  10. C#的控制台程序输出

    1. int nChar; string mystring; Console.WriteLine("{0} {1}",nChar,mystring); 其中{0},{1}为占位符 ...