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 ...
随机推荐
- win7快捷键和ubuntu快捷键
http://www.cnblogs.com/xfiver/archive/2010/12/08/1899905.html http://www.pc841.com/article/20121203- ...
- 邁向IT專家成功之路的三十則鐵律 鐵律二十七 IT人夢想之道-實踐
有句話說:「人因夢想而偉大」.身為IT的您不知道夢想為何?是希望能夠環遊世界.開一間咖啡廳.買一部法拉利跑車.買一部重機.中大樂透頭彩.娶心目中的女神當老婆,還是只要明天還能活著就好了.無論您的夢想為 ...
- Android在其他线程中更新UI
public class TransferTools { private static final int MSG_START = 1001; private static final int MSG ...
- tar命令中的-C作用
一直不知道解压命令如何指定文件夹,今天学到了一个 -C 参数 tar zxvf test.tar.gz -C test 注释:上面的命令将 test.tar.gz 这个压缩包解压到当前目录下的 tes ...
- hdu 4300 Clairewd’s message(具体解释,扩展KMP)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4300 Problem Description Clairewd is a member of FBI. ...
- python numpy实现多次循环读取文件 等间隔过滤数据
numpy的np.fromfile会出现如下的问题,只能一次性读取文件的内容,不能追加读取,连续两次的np.fromfile读到的东西一样 如果数据文件太大(几个G或以上)不能一次性全读进去,需要追加 ...
- binary-tree-maximum-path-sum——二叉树任意一条路径上的最大值
Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. ...
- xcode 5.0 以上去掉icon高亮方法&iOS5白图标问题
之前的建议方法是把在xxx.info.plist文件里把 icon already includes gloss and bevel effects 设置YES 在Xcode5下,重复实现不成功,今天 ...
- ffmpeg 跟我学 视频教程
最近一段时间找时间录制了一些Ffmpeg视频教程,还有录制完毕,会持续更新,内容会包含Ffmeg保存文件,网络流转发, 编码,解码,播放器制作,以及服务端搭建等等,适合初学者,有需要的朋友的可以关注: ...
- MVC上传多张图片
改变上传文件的按钮样式: <div id="post-upload-image"> <div id="divfile_-1"> < ...