The Tower of Babylon

来源:互联网 发布:淘宝汉服商家排名 编辑:程序博客网 时间:2024/04/28 12:55

Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu
Description
Download as PDF
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 tex2html_wrap_inline32 . 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 tex2html_wrap_inline40 , tex2html_wrap_inline42 and tex2html_wrap_inline44 .
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

思路:这道题的要求就是要你在他给的大小的长方体中拿来堆积,在上面的一块的长宽都要分别小与下面的一块。我也是在其他博客看得这种思路,先我们知道每一个长方体有6种状态,我们把他们都保存下来,然后对这些所有长方体的状态全部处理,这里我们在保存的时候是从大到小排个序,这里排序的条件是底面积大小,因为是面积小的不可能在下面(虽然面积大的也有可能不满足在上面的条件)。然后就FOR处理。

#include <iostream>#include <cstring>#include <cstdlib>#include <cstdio>#include <cmath>#include <algorithm>#include <queue>#include <stack>#include <iomanip>#include <map>#define inf 0x3f3f3f3fusing namespace std;class Node{    public:        int x,y,z;    void Insert(int a1,int b1,int c1)    {        x=a1;        y=b1;        z=c1;    }};bool cmp(const Node a,const Node b);Node *fox=new Node[200];//这里是类声明一个对象数组。int dp[200]={0};int main(){    int n,num=1;    while(cin>>n&&n)    {        int a,b,c,m=0,sum=0;        for(int i=0;i<n;i++)        {            scanf("%d %d %d",&a,&b,&c);            fox[m++].Insert(a,b,c);            fox[m++].Insert(a,c,b);            fox[m++].Insert(b,a,c);            fox[m++].Insert(b,c,a);            fox[m++].Insert(c,a,b);            fox[m++].Insert(c,b,a);        }//m最后就成了个数的总数。        sort(fox,fox+m,cmp);        memset(dp,0,sizeof(dp));        for(int i=0;i<m;i++)        {            dp[i]=fox[i].z;            for(int j=0;j<i;j++)            {                if(fox[i].x>fox[j].x&&fox[i].y>fox[j].y)                {                    dp[i]=max(dp[i],dp[j]+fox[i].z);                }            }            if(sum<dp[i])                sum=dp[i];        }        cout<<"Case "<<num++<<": maximum height = "<<sum<<endl;    }    return 0;}bool cmp(const Node a,const Node b)//结构体或者类的比较函数。{    return a.x*a.y<b.x*b.y;}
0 0
原创粉丝点击