UVa 437 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:html

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

 

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

 

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

Input is terminated by a value of zero (0) for n.this

 

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"spa

 

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

 

此題屬於最長上升子序列問題的變形。code

每次讀入一塊磚後,把它分紅六個角度存儲六次(一個長方體的磚會有三個不一樣的面,每一個面作底時都有兩種擺放的角度,一共六種),而後按底面的邊長從大到小排序,而後用最長上長子序列問題的狀態轉移方程求解便可,即設dp[i]表示從1到i的磚塊能夠堆的最高的塔的高度,有:orm

  dp[i] = max { height[i], dp[j]+height[i] | i兩條底邊比j的兩條底邊都短 }htm

 

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #include<cstring>
 5 
 6 using namespace std;
 7 
 8 struct BLOCK
 9 {
10     int x;
11     int y;
12     int z;
13 };
14 
15 int n;
16 int dp[200];
17 BLOCK b[200];
18 
19 bool cmp(BLOCK a,BLOCK b)
20 {
21     return a.x>b.x||(a.x==b.x&&a.y>b.y);
22 }
23 
24 int main()
25 {
26     int kase=0;
27 
28     while(scanf("%d",&n)==1&&n)
29     {
30         kase++;
31 
32         int t=0,_x,_y,_z;
33         for(int i=1;i<=n;i++)
34         {
35             scanf("%d %d %d",&_x,&_y,&_z);
36             b[t].x=_x;
37             b[t].y=_y;
38             b[t++].z=_z;
39             b[t].x=_y;
40             b[t].y=_x;
41             b[t++].z=_z;
42             b[t].x=_y;
43             b[t].y=_z;
44             b[t++].z=_x;
45             b[t].x=_z;
46             b[t].y=_y;
47             b[t++].z=_x;
48             b[t].x=_z;
49             b[t].y=_x;
50             b[t++].z=_y;
51             b[t].x=_x;
52             b[t].y=_z;
53             b[t++].z=_y;
54         }
55 
56         sort(b,b+t,cmp);
57 
58         memset(dp,0,sizeof(dp));
59 
60         for(int i=0;i<t;i++)
61         {
62             int ans=b[i].z;
63             for(int j=0;j<i;j++)
64                 if( ((b[i].x<b[j].x&&b[i].y<b[j].y)||(b[i].x<b[j].y&&b[i].y<b[j].x)) && dp[j]+b[i].z>ans )
65                     ans=dp[j]+b[i].z;
66             dp[i]=ans;
67         }
68 
69         int ans=0;
70         for(int i=0;i<t;i++)
71             if(dp[i]>ans)
72                 ans=dp[i];
73 
74         printf("Case %d: maximum height = %d\n",kase,ans);
75     }
76 
77     return 0;
78 }
[C++]
相關文章
相關標籤/搜索