Gym - 101147H H. Commandos —— DP
题目链接:http://codeforces.com/gym/101147/problem/H
题解:
单纯的三维DP。可用递推或记忆化搜索实现。
学习:开始时用记忆化搜索写,dp[]初始化为0,结果一直走不出循环。后来发现:即使被搜过的位置,其值也可以是0,当再一次访问这个位置时,由于其值为0,就误以为这个位置没有搜过,于是再搜一遍。所以就不能用0来判断是否被搜索过。以后记忆化搜索就用-1来初始化dp[]吧,当然最稳的做法就是加个vis[]数组,看情况而定。
递推:
- #include <iostream>
- #include <cstdio>
- #include <cstring>
- #include <cstdlib>
- #include <string>
- #include <vector>
- #include <map>
- #include <set>
- #include <queue>
- #include <sstream>
- #include <algorithm>
- using namespace std;
- #define pb push_back
- #define mp make_pair
- #define ms(a, b) memset((a), (b), sizeof(a))
- //#define LOCAL
- #define eps 0.0000001
- typedef long long LL;
- const int inf = 0x3f3f3f3f;
- const int maxn = 200000+10;
- const int mod = 1000000007;
- int dp[15][15][15];
- int a[15][15][15];
- void slove()
- {
- int N;
- int f,x,y,h;
- ms(a,0);
- ms(dp,0);
- scanf("%d",&N);
- while(N--)
- {
- scanf("%d%d%d%d",&f,&x,&y,&h);
- a[f][x][y] = h;
- }
- for(int k = 1; k<=10; k++)
- for(int i = 10; i>0; i--)
- for(int j = 10; j>0; j--)
- {
- 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] );
- }
- printf("%d\n",dp[10][1][1]);
- }
- int main()
- {
- #ifdef LOCAL
- freopen("commandos.in", "r", stdin);
- // freopen("output.txt", "w", stdout);
- #endif // LOCAL
- int T;
- scanf("%d", &T);
- while(T--){
- slove();
- }
- return 0;
- }
递归(记忆化搜索);
- #include <iostream>
- #include <cstdio>
- #include <cstring>
- #include <cstdlib>
- #include <string>
- #include <vector>
- #include <map>
- #include <set>
- #include <queue>
- #include <sstream>
- #include <algorithm>
- using namespace std;
- #define pb push_back
- #define mp make_pair
- #define ms(a, b) memset((a), (b), sizeof(a))
- //#define LOCAL
- #define eps 0.0000001
- typedef long long LL;
- const int inf = 0x3f3f3f3f;
- const int maxn = 200000+10;
- const int mod = 1000000007;
- int dp[15][15][15];
- int a[15][15][15];
- int dfs(int k, int x, int y)
- {
- if(k<1 || x>10 || y>10)
- return 0;
- if(dp[k][x][y]!=-1)// 初始化为-1
- return dp[k][x][y];
- 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) ) );
- return dp[k][x][y];
- }
- void slove()
- {
- int N;
- int f,x,y,h;
- ms(a,0);
- ms(dp,-1);
- scanf("%d",&N);
- while(N--)
- {
- scanf("%d%d%d%d",&f,&x,&y,&h);
- a[f][x][y] = h;
- }
- cout<<dfs(10,1,1)<<endl;
- }
- int main()
- {
- #ifdef LOCAL
- freopen("commandos.in", "r", stdin);
- // freopen("output.txt", "w", stdout);
- #endif // LOCAL
- int T;
- scanf("%d", &T);
- while(T--){
- slove();
- }
- return 0;
- }
Gym - 101147H H. Commandos —— DP的更多相关文章
- 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- ...
- 【动态规划】Gym - 101147H - Commandos
裸dp,看代码. #include<cstdio> #include<algorithm> #include<cstring> using namespace st ...
- codeforces gym 100357 H (DP 高精度)
题目大意 有r*s张扑克牌,数字从1到 r,每种数字有s种颜色. 询问对于所有随机的d张牌,能选出c张组成顺子的概率和组成同花的概率. 解题分析 对于组成顺子的概率,令dp[i][j][k]表示一共选 ...
- Codeforces Gym 100231L Intervals 数位DP
Intervals 题目连接: http://codeforces.com/gym/100231/attachments Description Start with an integer, N0, ...
- G - Surf Gym - 100819S -逆向背包DP
G - Surf Gym - 100819S 思路 :有点类似 逆向背包DP , 因为这些事件发生后是对后面的时间有影响. 所以,我们 进行逆向DP,具体 见代码实现. #include<bit ...
- 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 ...
- Gym 100952 H. Special Palindrome
http://codeforces.com/gym/100952/problem/H H. Special Palindrome time limit per test 1 second memory ...
- codeforces Gym 100187H H. Mysterious Photos 水题
H. Mysterious Photos Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/p ...
- codeforces Gym 100500H H. ICPC Quest 水题
Problem H. ICPC QuestTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100500/a ...
随机推荐
- 洛谷—— P2117 小Z的矩阵
https://www.luogu.org/problemnew/show/2117 题目描述 小Z最近迷上了矩阵,他定义了一个对于一种特殊矩阵的特征函数G.对于N*N的矩阵A,A的所有元素均为0或1 ...
- mybatis 源码学习(二)sqlsession
mybatis 中的sqlsession是一个非常重要的类.上篇我们分析了sessionfactory初始化配置文件,我们继续分析sessionfactory拿到会话进行的操作. 看这里.getMap ...
- Java ArrayList 详解
只记录目前为止关注的.JDK1.8 一.基础属性 1.1 内部参数 //空存储实例.直接new ArrayList()便是以该空数组作为实例 private static final Object[] ...
- Ubuntu 16.04下使用Wine安装PowerDesigner15
说明: 1.关于没有.wine文件夹的解决方法:在命令行上运行winecfg: 2.使用的Wine版本是深度出品(Deepin),已经精简了很多没用的配置,使启动能非常快,占用资源小. 下载: (链接 ...
- Leanote 二进制版详细安装教程 Windows
https://github.com/leanote/leanote/wiki 本教程适合 Windows 用户的二进制版安装. Windows 用户的源码版安装,参见这里. Mac, Linux 用 ...
- 深入理解Java中的HashMap的实现原理
HashMap继承自抽象类AbstractMap,抽象类AbstractMap实现了Map接口.关系图例如以下所看到的: Java中的Map<key, value>接口同意我们将一个对象作 ...
- 【转】supervisord使用
Supervisor (http://supervisord.org) 是一个用 Python 写的进程管理工具,可以很方便的用来启动.重启.关闭进程(不仅仅是 Python 进程).除了对单个进程的 ...
- json解析神器 jsonpath的使用
转载:http://blog.csdn.net/qq_20641565/article/details/77162868 如果项目需求是从某些复杂的json里面取值进行计算,用jsonpath+IK( ...
- PS 基础知识 什么是Adobe Bridge
Adobe Bridge是什么 悬赏分:0 - 解决时间:2007-2-23 10:50 下载的PS中附带了Adobe Bridge,可我不知道它是干什么用的?如何使用??? 谢谢! 提问者: Car ...
- require.js使用
无可奈何,二开项目用了require.js! 一道槛是挨不过去了 require官网: http://requirejs.org/ require.js cdn: <script src=&qu ...