题目链接: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;
}

最新文章

  1. 从Nodejs脚本到vue首页看开源始末的DemoHouse
  2. 【MVC 4】2.使用 Razor
  3. 【python】lamda表达式,map
  4. phalcon: (非官方)简单的多模块
  5. JS自执行函数的几种写法
  6. mac svn .a文件的上传方法
  7. poj2251 三维简单BFS
  8. XML学习笔记之XML的简介
  9. The Blocks Problem(vector)
  10. Spring-shiro源码陶冶-AuthorizingRealm用户认证以及授权
  11. PS图像特效算法——百叶窗
  12. 【转】数据库事务ACID以及事务隔离
  13. 深入jar包:从jar包中读取资源文件getResourceAsStream
  14. ubuntu16下安装openssh
  15. ffmpeg 加载双语字幕
  16. POJ - 1905 Expanding Rods(二分+计算几何)
  17. android本地数据库,微信数据库WCDB for Android 使用实例
  18. MVC登录校验
  19. c# 解析百度图片搜索结果json中objURL图片原始地址
  20. PAT甲题题解-1073. Scientific Notation (20)-字符串处理

热门文章

  1. 缓存区溢出漏洞工具Doona
  2. layui-时间选择器-时间范围选择
  3. 查询和设置mysql事务隔离级别
  4. Android自定义Dialog效果
  5. Android:MVC模式(下)
  6. elasticsearch 查询模板
  7. Nutch学习笔记二——抓取过程简析
  8. AngularJS的Foreach循环示例
  9. C 标准库 - &lt;signal.h&gt;
  10. [Python-tools]defaultdict的使用场景