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. Asteroids(匈牙利算法)

    求最小点覆盖数,即最大匹配数,匈牙利算法. #include<stdio.h> #include<string.h> ][],vis[],linker[];//linker[] ...

  2. centos vi和vim用法

    所有的 Unix Like 系统都会内建 vi 文书编辑器,其他的文书编辑器则不一定会存在. 但是目前我们使用比较多的是 vim 编辑器. vim 具有程序编辑的能力,可以主动的以字体颜色辨别语法的正 ...

  3. Django day05 虚拟环境 django 2.0和django 1.0 路由层区别

    一:虚拟环境 创建虚拟环境一般有三种方式: 1)   File--->New Project--> 出现如下图,点击Project Interpreter:New Virtualenv e ...

  4. C语言和C++的应用领域都在哪些?学C语言好,还是学习C++好?

    从事嵌入式开发十几年,基本上围绕着这两种编程语言展开,都可以直接操作底层的编程语言,用的越熟练越是感觉工具属性越强.虽然两种编程语言分属于不同的编程思想,用的时间长了觉得差异也不是很大,现在就个人的从 ...

  5. 利用poi,jxl将Excel数据导入数据库

    需求:‘需要将本地的Excel中的数据经过验证之后导入数据库,在导入数据库之前在页面上展示出来 思路:将Excel导入存到session里面 去判断有没有不合法数据  如果有阻止提交 工具类: imp ...

  6. Java_Web之分层架构

    当我们把业务处理的代码与JSP代码混在一起,不易于阅读,不易于代码维护,这就需要分层. 分层模式 1.分层模式是最常见的一种架构模式 2.分层模式是很多架构模式的基础 分层 将解决方案的组件分隔到不同 ...

  7. bootstrap 模态框日期控件datepicker被遮住问题的解决

    找到日期输入框,并将  .class 属性的 z-index 改大 在JSP页添加样式: 这样就OK了:

  8. 团体程序设计天梯赛-练习集-L1-035. 情人节

    L1-035. 情人节 以上是朋友圈中一奇葩贴:“2月14情人节了,我决定造福大家.第2个赞和第14个赞的,我介绍你俩认识…………咱三吃饭…你俩请…”.现给出此贴下点赞的朋友名单,请你找出那两位要请客 ...

  9. redis得配置及使用

    http://www.cnblogs.com/huskyking/p/6004772.html

  10. 有关微信小程序

    每个页面都要在app.json中配置 "pages": [ "pages/index/index", "pages/list/list", ...