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 ...
随机推荐
- ELK之收集Java日志、通过TCP收集日志
1.Java日志收集 使用codec的multiline插件实现多行匹配,这是一个可以将多行进行合并的插件,而且可以使用what指定将匹配到的行与前面的行合并还是和后面的行合并. 语法示例: inpu ...
- luogu P1922 女仆咖啡厅桌游吧
题目背景 小v带萌萌的妹妹去玩,妹妹想去女仆咖啡馆,小v想去桌游吧. 妹妹:“我问你个问题,答不对你就做我一天的奴隶,答对了就今天我就全部听你的.” 小v:“全部都听!?” 妹妹:“嘻嘻嘻,你还是回答 ...
- URAL - 1860 Fiborial
Discription Consider a sequence F i that satisfies the following conditions: Find the number of dif ...
- InnoDB: Warning: a long semaphore wait 解决办法
http://blog.csdn.net/wulantian/article/details/37560849
- tomcat访问(access)日志配置、记录Post请求参数
tomcat访问(access)日志配置.记录Post请求参数 一.配置与说明 tomcat访问日志格式配置,在config/server.xml里Host标签下加上 <Valve classN ...
- iOS -- SKTransition类
SKTransition类 继承自 NSObject 符合 NSObject(NSObject) 框架 /System/Library/Frameworks/SpriteKit.framewor ...
- SilverLight:基础控件使用(3)-DataGrid控件
ylbtech-SilverLight-Basic-Control:基础控件使用(3)-DataGrid控件 DataGrid控件-后台绑定 自动生成表列 不自动生成表列 1.A,返回顶部Person ...
- PS 如何使用抽出滤镜抠人物的头发丝等细节
1.打开图片,复制背景,关闭背景眼睛.单击 滤镜 -抽出, 我们要学会观察图片,先来看下面这张图: 这张图片色彩虽然不算丰富,但也不是纯色背景,甚至有些许的零乱,但人物的主题却被黑色长发包围, 我们只 ...
- C++11 并发指南系列(转)
本系列文章主要介绍 C++11 并发编程,计划分为 9 章介绍 C++11 的并发和多线程编程,分别如下: C++11 并发指南一(C++11 多线程初探)(本章计划 1-2 篇,已完成 1 篇) C ...
- 纯JS设置首页,增加收藏,获取URL參数,解决中文乱码
雪影工作室版权全部,转载请注明[http://blog.csdn.net/lina791211] 1.前言 纯Javascript 设置首页,增加收藏. 2.设置首页 // 设置为主页 functio ...