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  . 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.

Input and Output

The input file will contain one or more test cases. The first line of each test case contains an integer n, representing the number of different 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  ,  and  .

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"

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

Sample Output

Case 1: maximum height = 40
Case 2: maximum height = 21
Case 3: maximum height = 28
Case 4: maximum height = 342

题意:

或许你曾听过巴比伦塔的传说,现在这个故事的许多细节已经被遗忘了。现在,我们要告诉你整个故事:

巴比伦人有n种不同的积木,每种积木都是实心长方体,且数目都是无限的。第i种积木的长宽高分别为{ i , y i , z i }。积木可以被旋转,所以前面的长宽高是可以互相换的。也就是其中2个组成底部的长方形,剩下的一个为高度。巴比伦人想要尽可能的用积木来堆高塔,但是两块积木要叠在一起是有条件的:只有在第一块积木的底部2个边均小于第二块积木的底部相对的2个边时,第一块积木才可以叠在第二块积木上方。例如:底部为3x8的积木可以放在底部为4x10的积木上,但是无法放在底部为6x7的积木上。

给你一些积木的资料,你的任务是写一个程式算出可以堆出的塔最高是多少。

简单题意:

有n(n<=30)种立方体,每种都有无穷多个。要求选一些立方体摞成一根尽量高的柱子(可以自行选择那条边作为高),使得每个立方体的底面长宽分别严格小于它下方立方体的底面长宽

思路:其实题目中的每种立方体都有无限个是没大有的,因为你很容易就可以想到,每个立方体最多用三次。那么我们就可以把每个立方体分成三个高不同的立方体。如果一个立方体能放在另一个立方体上面,就在这两个立方体之间连边(有向边,底面小的向底面大的连边),这样就形成了一张有向图,最后,在图上搜索最长路作DAG上的最长路即可。

吐槽:其实这个题目仔细想想还是挺水的,但可能是我太菜的原因,竟然做了辣么久┭┮﹏┭┮

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int n,tot,num,ans,g[*][*],f[*];
struct nond{
int x,y,z;
}v[*];
void pre(){
for(int i=;i<=*n;i++)
for(int j=;j<=*n;j++){
if(i==j) continue;
if(v[i].x<v[j].x&&v[i].y<v[j].y||v[i].x<v[j].y&&v[i].y<v[j].x)
g[i][j]=;
}
}
int dfs(int x){
if(f[x]!=-) return f[x];
f[x]=v[x].z;
for(int i=;i<=*n;i++)
if(g[x][i])
f[x]=max(f[x],dfs(i)+v[x].z);
return f[x];
}
int main(){
while(scanf("%d",&n)&&n!=){
num++;ans=;tot=;
memset(v,,sizeof(v));
memset(g,,sizeof(g));
memset(f,-,sizeof(f));
for(int i=;i<=n;i++){
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
v[++tot].x=x;v[tot].y=y;v[tot].z=z;
v[++tot].x=y;v[tot].y=z;v[tot].z=x;
v[++tot].x=z;v[tot].y=x;v[tot].z=y;
}
pre();
for(int i=;i<=*n;i++)
ans=max(ans,dfs(i));
cout<<"Case "<<num<<": maximum height = ";
cout<<ans<<endl;
}
}

UVA The Tower of Babylon的更多相关文章

  1. uva The Tower of Babylon[LIS][dp]

    转自:https://mp.weixin.qq.com/s/oZVj8lxJH6ZqL4sGCXuxMw The Tower of Babylon(巴比伦塔) Perhaps you have hea ...

  2. UVA 437_The Tower of Babylon

    题意: 一堆石头,给定长宽高,每种石头均可以使用无数次,问这堆石头可以叠放的最高高度,要求下面的石头的长和宽分别严格大于上面石头的长和宽. 分析: 采用DAG最长路算法,由于长宽较大,不能直接用于表示 ...

  3. UVA 437 十九 The Tower of Babylon

    The Tower of Babylon Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Subm ...

  4. UVa 437 The Tower of Babylon(经典动态规划)

    传送门 Description Perhaps you have heard of the legend of the Tower of Babylon. Nowadays many details ...

  5. UVa 437 The Tower of Babylon

    Description   Perhaps you have heard of the legend of the Tower of Babylon. Nowadays many details of ...

  6. UVa 437 The Tower of Babylon(DP 最长条件子序列)

     题意  给你n种长方体  每种都有无穷个  当一个长方体的长和宽都小于还有一个时  这个长方体能够放在还有一个上面  要求输出这样累积起来的最大高度 由于每一个长方体都有3种放法  比較不好控制 ...

  7. POJ2241——The Tower of Babylon

    The Tower of Babylon Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2207   Accepted: 1 ...

  8. UVA437-The Tower of Babylon(动态规划基础)

    Problem UVA437-The Tower of Babylon Accept: 3648  Submit: 12532Time Limit: 3000 mSec Problem Descrip ...

  9. DAG 动态规划 巴比伦塔 B - The Tower of Babylon

    题目:The Tower of Babylon 这是一个DAG 模型,有两种常规解法 1.记忆化搜索, 写函数,去查找上一个符合的值,不断递归 2.递推法 方法一:记忆化搜索 #include < ...

随机推荐

  1. B5090 组题 二分答案

    bzoj有毒,看不了自己哪错了...根本没法debug. 我到现在还是不知道自己代码为什么会T,二分次数也加限制了,但是还是T...救命啊!!! 题干: Description 著名出题人小Q的备忘录 ...

  2. 自顶向下(递归)的归并排序和自底向上(循环)的归并排序——java实现

    归并排序有两种实现方式,自顶向下和自底向上.前者的思想是分治法,现将数组逐级二分再二分,分到最小的两个元素后,逐级往上归并,故其核心在于归并.后者的思想相反,采用循环的方式将小问题不断的壮大,最后变成 ...

  3. 一个豆瓣 API 的反向代理配置,旨在解决豆瓣屏蔽小程序请求问题(豆瓣接口 403 问题)

    #user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #erro ...

  4. PCB 工程系统 模拟windows域帐号登入

    一.需求描述: 对于PCB制造企业来说,基本都采用建立共享目享+域名管控权限,好像别的大多数行业都是这样的吧.呵呵 在实际应用中,经常会有这样的问题,自己登入的帐号没有共享目录的权限,但又想通过程序实 ...

  5. codevs1288 埃及分数(IDA*)

    1288 埃及分数  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond     题目描述 Description 在古埃及,人们使用单位分数的和(形如1/a的 ...

  6. Android 关于Fragment重叠问题分析和解决

    一.问题描述 相信大家在使用Fragment的过程中,肯定碰到过Fragment重叠的问题,重启应用就好了.然而原因是什么呢? 二.原因分析 首先,Android管理Fragment有两种方式,使用a ...

  7. Unity引擎GUI之Image

    UGUI的Image等价于NGUI的Sprite组件,用于显示图片. 一.Image组件: Source Image(图像源):纹理格式为Sprite(2D and UI)的图片资源(导入图片后选择T ...

  8. php常见报错

    Php常见错误提示 一.Fatal error: Call to undefined function……函数不存在,可能的原因:系统不存在这个函数且你也没自定义 二.syntax error, un ...

  9. SSDB 一个高性能的支持丰富数据结构的 NoSQL 数据库, 用于替代 Redis.

    SSDB 一个高性能的支持丰富数据结构的 NoSQL 数据库, 用于替代 Redis. 特性 替代 Redis 数据库, Redis 的 100 倍容量 LevelDB 网络支持, 使用 C/C++ ...

  10. C++多行文本读取

    使用的多行读取的代码如下: //读取文本浮点数到多个模式 序列 bool CPicToolsDlg::readTxt2SeqMulti( std::string TxtName, std::vecto ...