UVA The Tower of Babylon

Posted 一蓑烟雨任生平

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UVA The Tower of Babylon相关的知识,希望对你有一定的参考价值。

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[40*3][40*3],f[40*3];
struct nond{
    int x,y,z;
}v[40*3];
void pre(){
    for(int i=1;i<=3*n;i++)
        for(int j=1;j<=3*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]=1;
        }
}
int dfs(int x){
    if(f[x]!=-1)    return f[x];
    f[x]=v[x].z;
    for(int i=1;i<=3*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!=0){
        num++;ans=0;tot=0;
        memset(v,0,sizeof(v));
        memset(g,0,sizeof(g));
        memset(f,-1,sizeof(f));
        for(int i=1;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=1;i<=3*n;i++)
            ans=max(ans,dfs(i));
        cout<<"Case "<<num<<": maximum height = ";
        cout<<ans<<endl;
    }
}

 

以上是关于UVA The Tower of Babylon的主要内容,如果未能解决你的问题,请参考以下文章

Uva437 The Tower of Babylon

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

uva 437 The Tower of Babylon

UVA437 The Tower of Babylon

The Tower of Babylon UVA - 437

uva437-The Tower of Babylon