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. Codeforces--106C--Buns(背包)

    Buns Time Limit: 2000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Submit Status ...

  2. APP-Android:APK

    ylbtech-APP-Android:APK APK是AndroidPackage的缩写,即Android安装包(apk).APK是类似Symbian Sis或Sisx的文件格式.通过将APK文件直 ...

  3. PCB 电测试--测试点数自动输出到流程指示中(读取TGZ Stephdr文件)

    好不容易实现了 <PCB 无需解压,直接读取Genesis TGZ指定文件 > 正好这里有一项需求:PCB电测试--测试点数自动输出到流程指示中 一.自动输出测试点小结; 1.由于历史原因 ...

  4. Java IO流文件复制/解压的几种方法总结

    引言 在JavaWeb项目开发过程,涉及到IO文件的读写操作以及文件的复制copy操作是作为一个程序员不可获取的知识,那接下来就总结一些copy文件的一些方法,与大家通过学习,如果还有其他更好的方法, ...

  5. Node.js文件系统Api总结

    //公共引用 var fs = require('fs'), path = require('path'); 1.读取文件readFile函数 //readFile(filename,[options ...

  6. poj2376 Cleaning Shifts 区间贪心

    题目大意: (不说牛了) 给出n个区间,选出个数最少的区间来覆盖区间[1,t].n,t都是给出的. 题目中默认情况是[1,x],[x+1,t]也是可以的.也就是两个相邻的区间之间可以是小区间的右端与大 ...

  7. ★Java语法(七)——————————循环语句

    package 自我总结; public class 循环语句 { public static void main(String[] args) { // while 循环 // 用法: // whi ...

  8. 09--c++ 类的继承与派生

    c++ 类的继承与派生   一.基本概念 1.类的继承,是新的类从已有类那里得到已有的特性.或从已有类产生新类的过程就是类的派生.原有的类称为基类或父类,产生的新类称为派生类或子类.   2.派生类的 ...

  9. (转)基于Metronic的Bootstrap开发框架经验总结(3)--下拉列表Select2插件的使用

    http://www.cnblogs.com/wuhuacong/p/4761637.html 在上篇<基于Metronic的Bootstrap开发框架经验总结(2)--列表分页处理和插件JST ...

  10. [iOS Reverse]logify日志追踪,锁定注入口-控制台查看

    前言 logify是theos的一个组件,路径是: /opt/theos/bin/logify.pl 我们还是以微信红包为例子,根据[iOS Hacking]运行时分析cycript得到的入口文件: ...