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]分别表示第几根棒子,这根棒子的高度,是否达到题目的要求和使用不同棒子数.那么接下来就 ...
随机推荐
- MVC小系列(二)【Razor 模板引擎】
Razor 模板引擎 Razor模板页:它使我们不用再使用master模板了 一 :@Url.Content:是可以加载CSS和JS等文件比如: <link href="@Url.Co ...
- A题笔记(11)
No.1508 代码:https://code.csdn.net/snippets/192058 考察点:①char型字符转换成对应的 ASKII 的编码 可以通过这样对 string 的每一个字符 ...
- STORM 免费且开源的WebSerivce测试工具
一.名称 STORM 是一款免费且开源的WebSerivce测试工具 二.使用方式 1.发布自己的webservice服务 例如:http://www.webxml.com.cn/WebService ...
- Solr4.8.1与Tomcat7整合
Solr4.8.1和Tomcat7都可以到官方网站去下载,我这里就不多说了,如下图. 这里我们首先解压Solr-4.8.1.zip,再解压Tomcat,解压后,再在当前文件夹下建2个文件夹,一个用来放 ...
- AudioStreamer使用之快速点击下/上一首按钮,音频会重复的问题解决。
针对此问题,我的做法是在AudioStreamer.h文件新加入一个属性 @property (nonatomic,strong) NSURL *musicUrl; 和一个单例方法: +(id)sha ...
- c# web页面乱码
1.在web.config中加入:<globalization requestEncoding="GB2312" responseEncoding="GB2312& ...
- JSP EL表达式详细介绍
一.JSP EL语言定义 E L(Expression Language) 目的:为了使JSP写起来更加简单. 表达式语言的灵感来自于 ECMAScript 和 XPath 表达式语言,它提供了在 ...
- 通过 OpenNI 建立 Kinect 3D Point Cloud
這篇還是算延續前一篇的<透過 OpneNI 合併 Kinect 深度以及彩色影像資料>.在可以透過 OpenNI 讀取到 Kinect 的深度.色彩資訊之後,其實就可以試著用這些資訊,來重 ...
- 263. Ugly Number(C++)
263. Ugly Number Write a program to check whether a given number is an ugly number. Ugly numbers are ...
- 使用CPA4破解经典密码算法
下面是一段经过经典密码算法加密的密文(加密算法未知): yvvnerujjvnywhbdvkpchfgvjtzwqsuporqfzpoekkjgziicdwwkeejdsruef whwseyej ...