题目链接:http://codeforces.com/gym/101147/problem/H

题解:

单纯的三维DP。可用递推或记忆化搜索实现。

学习:开始时用记忆化搜索写,dp[]初始化为0,结果一直走不出循环。后来发现:即使被搜过的位置,其值也可以是0,当再一次访问这个位置时,由于其值为0,就误以为这个位置没有搜过,于是再搜一遍。所以就不能用0来判断是否被搜索过。以后记忆化搜索就用-1来初始化dp[]吧,当然最稳的做法就是加个vis[]数组,看情况而定。

递推:

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <cstdlib>
  5. #include <string>
  6. #include <vector>
  7. #include <map>
  8. #include <set>
  9. #include <queue>
  10. #include <sstream>
  11. #include <algorithm>
  12. using namespace std;
  13. #define pb push_back
  14. #define mp make_pair
  15. #define ms(a, b) memset((a), (b), sizeof(a))
  16. //#define LOCAL
  17. #define eps 0.0000001
  18. typedef long long LL;
  19. const int inf = 0x3f3f3f3f;
  20. const int maxn = 200000+10;
  21. const int mod = 1000000007;
  22. int dp[15][15][15];
  23. int a[15][15][15];
  24. void slove()
  25. {
  26. int N;
  27. int f,x,y,h;
  28. ms(a,0);
  29. ms(dp,0);
  30. scanf("%d",&N);
  31. while(N--)
  32. {
  33. scanf("%d%d%d%d",&f,&x,&y,&h);
  34. a[f][x][y] = h;
  35. }
  36.  
  37. for(int k = 1; k<=10; k++)
  38. for(int i = 10; i>0; i--)
  39. for(int j = 10; j>0; j--)
  40. {
  41. dp[k][i][j] = a[k][i][j] + max( max(dp[k][i][j+1], dp[k][i+1][j]), dp[k-1][i][j] );
  42. }
  43.  
  44. printf("%d\n",dp[10][1][1]);
  45. }
  46. int main()
  47. {
  48. #ifdef LOCAL
  49. freopen("commandos.in", "r", stdin);
  50. // freopen("output.txt", "w", stdout);
  51. #endif // LOCAL
  52.  
  53. int T;
  54. scanf("%d", &T);
  55. while(T--){
  56. slove();
  57. }
  58.  
  59. return 0;
  60. }

递归(记忆化搜索);

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <cstdlib>
  5. #include <string>
  6. #include <vector>
  7. #include <map>
  8. #include <set>
  9. #include <queue>
  10. #include <sstream>
  11. #include <algorithm>
  12. using namespace std;
  13. #define pb push_back
  14. #define mp make_pair
  15. #define ms(a, b) memset((a), (b), sizeof(a))
  16. //#define LOCAL
  17. #define eps 0.0000001
  18. typedef long long LL;
  19. const int inf = 0x3f3f3f3f;
  20. const int maxn = 200000+10;
  21. const int mod = 1000000007;
  22. int dp[15][15][15];
  23. int a[15][15][15];
  24.  
  25. int dfs(int k, int x, int y)
  26. {
  27. if(k<1 || x>10 || y>10)
  28. return 0;
  29.  
  30. if(dp[k][x][y]!=-1)// 初始化为-1
  31. return dp[k][x][y];
  32.  
  33. dp[k][x][y] = a[k][x][y] + max( dfs(k-1,x,y), max( dfs(k,x+1,y) ,dfs(k,x,y+1) ) );
  34.  
  35. return dp[k][x][y];
  36. }
  37.  
  38. void slove()
  39. {
  40. int N;
  41. int f,x,y,h;
  42. ms(a,0);
  43. ms(dp,-1);
  44. scanf("%d",&N);
  45. while(N--)
  46. {
  47. scanf("%d%d%d%d",&f,&x,&y,&h);
  48. a[f][x][y] = h;
  49. }
  50. cout<<dfs(10,1,1)<<endl;
  51. }
  52. int main()
  53. {
  54. #ifdef LOCAL
  55. freopen("commandos.in", "r", stdin);
  56. // freopen("output.txt", "w", stdout);
  57. #endif // LOCAL
  58. int T;
  59. scanf("%d", &T);
  60. while(T--){
  61. slove();
  62. }
  63.  
  64. return 0;
  65. }

Gym - 101147H H. Commandos —— DP的更多相关文章

  1. Gym - 100341C FFT优化DP

    题目链接:传送门 题解: 设定dp[i][j]在深度为i下,使用j个节点的方案数 显然的转移方程组就是 dp[h][n] = dp[h-1][i] * dp[h-1][n-i-1] + 2*dp[h- ...

  2. 【动态规划】Gym - 101147H - Commandos

    裸dp,看代码. #include<cstdio> #include<algorithm> #include<cstring> using namespace st ...

  3. codeforces gym 100357 H (DP 高精度)

    题目大意 有r*s张扑克牌,数字从1到 r,每种数字有s种颜色. 询问对于所有随机的d张牌,能选出c张组成顺子的概率和组成同花的概率. 解题分析 对于组成顺子的概率,令dp[i][j][k]表示一共选 ...

  4. Codeforces Gym 100231L Intervals 数位DP

    Intervals 题目连接: http://codeforces.com/gym/100231/attachments Description Start with an integer, N0, ...

  5. G - Surf Gym - 100819S -逆向背包DP

    G - Surf Gym - 100819S 思路 :有点类似 逆向背包DP , 因为这些事件发生后是对后面的时间有影响. 所以,我们 进行逆向DP,具体 见代码实现. #include<bit ...

  6. Gym 102056I - Misunderstood … Missing - [DP][The 2018 ICPC Asia-East Continent Final Problem I]

    题目链接:https://codeforces.com/gym/102056/problem/I Warm sunshine, cool wind and a fine day, while the ...

  7. Gym 100952 H. Special Palindrome

    http://codeforces.com/gym/100952/problem/H H. Special Palindrome time limit per test 1 second memory ...

  8. codeforces Gym 100187H H. Mysterious Photos 水题

    H. Mysterious Photos Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/p ...

  9. codeforces Gym 100500H H. ICPC Quest 水题

    Problem H. ICPC QuestTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100500/a ...

随机推荐

  1. 洛谷—— P2117 小Z的矩阵

    https://www.luogu.org/problemnew/show/2117 题目描述 小Z最近迷上了矩阵,他定义了一个对于一种特殊矩阵的特征函数G.对于N*N的矩阵A,A的所有元素均为0或1 ...

  2. mybatis 源码学习(二)sqlsession

    mybatis 中的sqlsession是一个非常重要的类.上篇我们分析了sessionfactory初始化配置文件,我们继续分析sessionfactory拿到会话进行的操作. 看这里.getMap ...

  3. Java ArrayList 详解

    只记录目前为止关注的.JDK1.8 一.基础属性 1.1 内部参数 //空存储实例.直接new ArrayList()便是以该空数组作为实例 private static final Object[] ...

  4. Ubuntu 16.04下使用Wine安装PowerDesigner15

    说明: 1.关于没有.wine文件夹的解决方法:在命令行上运行winecfg: 2.使用的Wine版本是深度出品(Deepin),已经精简了很多没用的配置,使启动能非常快,占用资源小. 下载: (链接 ...

  5. Leanote 二进制版详细安装教程 Windows

    https://github.com/leanote/leanote/wiki 本教程适合 Windows 用户的二进制版安装. Windows 用户的源码版安装,参见这里. Mac, Linux 用 ...

  6. 深入理解Java中的HashMap的实现原理

    HashMap继承自抽象类AbstractMap,抽象类AbstractMap实现了Map接口.关系图例如以下所看到的: Java中的Map<key, value>接口同意我们将一个对象作 ...

  7. 【转】supervisord使用

    Supervisor (http://supervisord.org) 是一个用 Python 写的进程管理工具,可以很方便的用来启动.重启.关闭进程(不仅仅是 Python 进程).除了对单个进程的 ...

  8. json解析神器 jsonpath的使用

    转载:http://blog.csdn.net/qq_20641565/article/details/77162868 如果项目需求是从某些复杂的json里面取值进行计算,用jsonpath+IK( ...

  9. PS 基础知识 什么是Adobe Bridge

    Adobe Bridge是什么 悬赏分:0 - 解决时间:2007-2-23 10:50 下载的PS中附带了Adobe Bridge,可我不知道它是干什么用的?如何使用??? 谢谢! 提问者: Car ...

  10. require.js使用

    无可奈何,二开项目用了require.js! 一道槛是挨不过去了 require官网: http://requirejs.org/ require.js cdn: <script src=&qu ...