UVA_437_The_Tower_of_the_Babylon_(DAG上动态规划/记忆化搜索)
描述
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=378
n种方块,给出每一种的长宽高,现在要落起来,上面的方块的长和宽要严格小于下面的方块,问最多落多高.
ACM Contest Problems Archive
University of Valladolid (SPAIN)
437 The Tower of Babylon
Perhaps you have heard of the legend of the Tower of Babylon. Nowadays many details of this tale have
been forgotten. So now, in line with the educational nature of this contest, we will tell you the whole
story:
The babylonians had n types of blocks, and an unlimited supply of blocks of each type. Each type- i
block was a rectangular solid with linear dimensions ( x ; y ; z ). A block could be reoriented so that
any two of its three dimensions determined the dimensions of the base and the other dimension was the
height. They wanted to construct the tallest tower possible by stacking blocks. The problem was that,
in building a tower, one block could only be placed on top of another block as long as the two base
dimensions of the upper block were both strictly smaller than the corresponding base dimensions of the
lower block. This meant, for example, that blocks oriented to have equal-sized bases couldn't be stacked.
Your job is to write a program that determines the height of the tallest tower the babylonians can
build with a given set of blocks.
i
i
i
Input and Output
The input le will contain one or more test cases. The rst line of each test case contains an integer n ,
representing the number of dierent blocks in the following data set. The maximum value for n is 30.
Each of the next n lines contains three integers representing the values x , y and z .
Input is terminated by a value of zero (0) for n .
For each test case, print one line containing the case number (they are numbered sequentially starting
from 1) and the height of the tallest possible tower in the format " Case case : maximum height =
height "
i
Sample Input
1
10 20 30
2
6 8 10
5 5 5
7
1 1 1
2 2 2
3 3 3
4 4 4
5 5 5
6 6 6
7 7 7
5
31 41 59
26 53 58
97 93 23
84 62 64
33 83 27
0
i
iACM Contest Problems Archive
Sample Output
Case
Case
Case
Case
1:
2:
3:
4:
maximum
maximum
maximum
maximum
height
height
height
height
=
=
=
=
40
21
28
342
分析
直观的想法就是暴搜,但是有重叠自问题,所以可以做记忆化.但是用长度来做数组下标不切实际,所以有(idx,k)这个二元组表示第idx个方块,以第k条边作为高的情况,然后记忆化搜索即可.
注意:
1.对于一个点,搜索完了之后再加上他自己的高度.
#include <bits/stdc++.h>
using namespace std; const int maxn=+;
int n,kase;
int a[maxn][],dp[maxn][]; int dfs(int idx,int k){
int &ans=dp[idx][k];
if(ans) return ans;
int x1=a[idx][(k+)%],y1=a[idx][(k+)%];
for(int i=;i<=n;i++)
for(int j=;j<;j++){
int x2=a[i][(j+)%],y2=a[i][(j+)%];
if(x2<x1&&y2<y1||x2<y1&&y2<x1) ans=max(ans,dfs(i,j));
}
ans+=a[idx][k];
return ans;
}
void init(){
memset(dp,,sizeof dp);
for(int i=;i<=n;i++)
for(int j=;j<;j++)
scanf("%d",&a[i][j]);
}
int main(){
while(scanf("%d",&n)&&n){
init();
int ans=;
for(int i=;i<=n;i++)
for(int j=;j<;j++)
ans=max(ans,dfs(i,j));
printf("Case %d: maximum height = %d\n",++kase,ans);
}
return ;
}
UVA_437_The_Tower_of_the_Babylon_(DAG上动态规划/记忆化搜索)的更多相关文章
- sicily 1176. Two Ends (Top-down 动态规划+记忆化搜索 v.s. Bottom-up 动态规划)
Description In the two-player game "Two Ends", an even number of cards is laid out in a ro ...
- Codevs_1017_乘积最大_(划分型动态规划/记忆化搜索)
描述 http://codevs.cn/problem/1017/ 给出一个n位数,在数字中间添加k个乘号,使得最终的乘积最大. 1017 乘积最大 2000年NOIP全国联赛普及组NOIP全国联赛提 ...
- Poj-P1088题解【动态规划/记忆化搜索】
本文为原创,转载请注明:http://www.cnblogs.com/kylewilson/ 题目出处: http://poj.org/problem?id=1088 题目描述: 区域由一个二维数组给 ...
- [NOIP2017] 逛公园 (最短路,动态规划&记忆化搜索)
题目链接 Solution 我只会60分暴力... 正解是 DP. 状态定义: \(f[i][j]\) 代表 \(1\) 到 \(i\) 比最短路长 \(j\) 的方案数. 那么很显然最后答案也就是 ...
- 滑雪---poj1088(动态规划+记忆化搜索)
题目链接:http://poj.org/problem?id=1088 有两种方法 一是按数值大小进行排序,然后按从小到大进行dp即可: #include <iostream> #incl ...
- 由DAG到背包问题——记忆化搜索和递推两种解法
一.问题描述 物品无限的背包问题:有n种物品,每种均有无穷多个.第 i 种物品的体积为Vi,重量为Wi.选一些物品装到一个容量为 C 的背包中,求使得背包内物品总体积不超过C的前提下重量的最大值.1≤ ...
- 专题1:记忆化搜索/DAG问题/基础动态规划
A OpenJ_Bailian 1088 滑雪 B OpenJ_Bailian 1579 Function Run Fun C HDU 1078 FatMouse and Chee ...
- LightOJ1417 Forwarding Emails(强连通分量+缩点+记忆化搜索)
题目大概是,每个人收到信息后会把信息发给他认识的一个人如此下去,问一开始要把信息发送给谁这样看到信息的人数最多. 首先找出图中的SCC并记录每个SCC里面的点数,如果传到一个SCC,那么里面的人都可以 ...
- poj1351Number of Locks(记忆化搜索)
题目链接: 传送门 思路: 这道题是维基百科上面的记忆化搜索的例题... 四维状态dp[maxn][5][2][5]分别表示第几根棒子,这根棒子的高度,是否达到题目的要求和使用不同棒子数.那么接下来就 ...
随机推荐
- 那天有个小孩跟我说LINQ(二)转载
1 LINQ TO Objects续(代码下载) 新建项目 linq_Ch2控制台程序,新建一个Entity文件夹 1.1 学生成绩查询(练习Join) 有三张表如下 ...
- 关于bootstrap的datepicker在meteor应用中的使用(不包含bootstrap框架)
1.安装bootstrap3-datepicker包 meteor add rajit:bootstrap3-datepicker 2.使用方法 Example In your handlebars ...
- Java Web应用启动间隔执行的程序
Reference:<Java定时器timer.schedule在Web中间隔执行任务和定时><[Java]Timer和TimerTask详解> 做了一个Demo,完成如下的功 ...
- Ext.Net学习笔记06:Ext.Net DirectEvents用方补充
在ASP.NET控件上面使用DirectEvents 我们在ASP.NET中实现无刷新的页面请求的时候,通常会用到UpdatePanel,现在Ext.Net为我们提供了另外一种渠道:通过DirectE ...
- 2015 Multi-University Training Contest 1 题解&&总结
---------- HDU 5288 OO’s Sequence 题意 给定一个数列(长度<$10^5$),求有多少区间[l,r],且区间内有多少数,满足区间内其它数不是他的约数. 数的范围$ ...
- 【CODECHEF】【phollard rho + miller_rabin】The First Cube
All submissions for this problem are available. Read problems statements in Mandarin Chinese and Rus ...
- c# 与 winform 界面开发
在 windows 下使用 vs2010 开发,未深入研究. c# 与 .net 开发,一堆又一堆的新名词,头晕目眩,比如 CLR / apartments / STA / MTA / COM 吐槽无 ...
- 九度OJ 1452 搬寝室 -- 动态规划
题目地址:http://ac.jobdu.com/problem.php?pid=1452 题目描述: 搬寝室是很累的,xhd深有体会.时间追述2006年7月9号,那天xhd迫于无奈要从27号楼搬到3 ...
- spark - 将RDD保存到RMDB(MYSQL)数据库中
SCALA连接数据库批量插入: scala> import java.sql.DriverManager scala> var url = "jdbc:mysql://local ...
- C++输入输出缓冲区的刷新问题
当我们对文件流进行操作的时候,它们与一个streambuf 类型的缓存(buffer)联系在一起.这个缓存(buffer)实际是一块内存空间,作为流(stream)和物理文件的媒介.例如,对于一个输出 ...