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]分别表示第几根棒子,这根棒子的高度,是否达到题目的要求和使用不同棒子数.那么接下来就 ...
随机推荐
- Visual C++ 打印编程技术-打印基础知识
打印机介绍 1.打印术语 *: 1 英寸= 2.54 厘米(cm)= 25.4 毫米(mm) cpi (Characters Per Inch): 每英寸内所含的字符数,用来表示字符的大小.间距 cp ...
- JavaScript高级程序设计(一):JavaScript简介
一.JavaScript实现 1.一个完整的JavaScript包含三个部分组成: 1)ECMAScript 核心 2)DOM文档对象模型 3)BOM浏览器对象模型 2.文档对象模型(DOM) 文档对 ...
- swift-01-利用元组判断字符串出现次数
//问题的提出:有一个字符串 array = ["1","2","4","4","2"," ...
- erlang 练手 进程环
Erlang 编程指南第四章 练习4-2 编写一个程序,生成N个进程并相连成环,启动环后绕环发送M个消息,当收到退出消息后终止. ringStart(M,N,Message, Cp) -> io ...
- 《sed的流艺术之四》-linux命令五分钟系列之二十四
本原创文章属于<Linux大棚>博客,博客地址为http://roclinux.cn.文章作者为rocrocket. 为了防止某些网站的恶性转载,特在每篇文章前加入此信息,还望读者体谅. ...
- EF初始化mysql数据库codefirst
EF使用Code First修改生成数据库表名的方法 1. 重写OnModelCreating,去掉表名复数 System.Data.Entity.ModelConfiguration.Convent ...
- Android中图片的异步加载
转: 1. 为什么要异步加载图片 下载图片比较费时,先显示文字部分,让加载图片的过程在后台,以提升用户体验 2. SoftReference的作用 栈内存—引用 堆内存—对象 Eg: Object ...
- event的属性
t获取鼠标相对于浏览器左上角的坐标 <div id="dv" style=" width:300px; height:200px; background-color ...
- PHP 易出问题记录
PHP foreach引用缺陷 <?php $array = array(1, 2, 3); foreach ($array as &$v) {} foreach ($array as ...
- [转]Python中的矩阵转置
Python中的矩阵转置 via 需求: 你需要转置一个二维数组,将行列互换. 讨论: 你需要确保该数组的行列数都是相同的.比如: arr = [[1, 2, 3], [4, 5, 6], [7, 8 ...