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 ...
随机推荐
- 查找系列合集-二叉查找树BST
一. 二叉树 1. 什么是二叉树? 在计算机科学中,二叉树是每个结点最多有两个子树的树结构. 通常子树被称作“左子树”(left subtree)和“右子树”(right subtree). 二叉树常 ...
- POJ 2253 Frogger Floyd
原题链接:http://poj.org/problem?id=2253 Frogger Time Limit: 1000MS Memory Limit: 65536K Total Submissi ...
- CTSC2017游记&心得记
先来占个坑,骗点访问量 相册地址,戳这里 Day-1 一大清早就被叫了起来,赶去回车站....结果到了那里发现早了快1h?exm?是谁一早清早扰人清梦QAQ 杭州东转车,看到5号检票口被乘警团团围了起 ...
- 天天算法————快排及java实现。
快排说的很邪乎,原理懂了,实现自然也就出来了: public void static quickSorted( int[] a ,int low ,int high){ //递归结束条件 if(low ...
- Java线程池的内部实现
一.线程池介绍 线程是稀缺资源,如果无限制的创建,不仅会消耗系统资源,还会降低系统的稳定性,合理的使用线程池可以对线程进行统一的分配.调优和监控,并有以下好处: (1)降低资源消耗. (2)提高响应速 ...
- hadoop之hdfs及其工作原理
hadoop之hdfs及其工作原理 (一)hdfs产生的背景 随着数据量的不断增大和增长速度的不断加快,一台机器上已经容纳不下,因此就需要放到更多的机器中,但这样做不方便维护和管理,因此需要一种文件系 ...
- Java创建和解析Json数据方法(四)——json-lib包的使用
(四)json-lib包的使用 既然json-lib包比org.json包重量级,那么json-lib包肯定有很多org.json包没有的类和方法,这篇笔记简单记录json-lib包中 ...
- Linux下查看某个命令的参数
1.一般每个命令都带有help参数,使用方法如下: shutdown --help 提示:shutdown为关机命令,在真实环境使用时需要root权限,比如前面加sudo. 2.使用man命令查看,使 ...
- 【IntelliJ IDEA】idea显示工具栏
idea显示工具栏 在view->勾选对应按钮即可
- iOS -- 开源项目和库
TimLiu-iOS 目录 UI 下拉刷新 模糊效果 AutoLayout 富文本 图表 表相关与Tabbar 隐藏与显示 HUD与Toast 对话框 其他UI 动画 侧滑与右滑返回手势 gif动画 ...