UVA_437_The_Tower_of_the_Babylon_(DAG上动态规划/记忆化搜索)
描述
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=378
n种方块,给出每一种的长宽高,现在要落起来,上面的方块的长和宽要严格小于下面的方块,问最多落多高.
ACM Contest Problems Archive
University of Valladolid (SPAIN)
437 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 ( x ; y ; z ). 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.
i
i
i
Input and Output
The input le will contain one or more test cases. The rst line of each test case contains an integer n ,
representing the number of dierent 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 x , y and z .
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 "
i
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
i
iACM Contest Problems Archive
Sample Output
Case
Case
Case
Case
1:
2:
3:
4:
maximum
maximum
maximum
maximum
height
height
height
height
=
=
=
=
40
21
28
342
分析
直观的想法就是暴搜,但是有重叠自问题,所以可以做记忆化.但是用长度来做数组下标不切实际,所以有(idx,k)这个二元组表示第idx个方块,以第k条边作为高的情况,然后记忆化搜索即可.
注意:
1.对于一个点,搜索完了之后再加上他自己的高度.
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 const int maxn=30+5; 5 int n,kase; 6 int a[maxn][3],dp[maxn][3]; 7 8 int dfs(int idx,int k){ 9 int &ans=dp[idx][k]; 10 if(ans) return ans; 11 int x1=a[idx][(k+1)%3],y1=a[idx][(k+2)%3]; 12 for(int i=1;i<=n;i++) 13 for(int j=0;j<3;j++){ 14 int x2=a[i][(j+1)%3],y2=a[i][(j+2)%3]; 15 if(x2<x1&&y2<y1||x2<y1&&y2<x1) ans=max(ans,dfs(i,j)); 16 } 17 ans+=a[idx][k]; 18 return ans; 19 } 20 void init(){ 21 memset(dp,0,sizeof dp); 22 for(int i=1;i<=n;i++) 23 for(int j=0;j<3;j++) 24 scanf("%d",&a[i][j]); 25 } 26 int main(){ 27 while(scanf("%d",&n)&&n){ 28 init(); 29 int ans=0; 30 for(int i=1;i<=n;i++) 31 for(int j=0;j<3;j++) 32 ans=max(ans,dfs(i,j)); 33 printf("Case %d: maximum height = %d\n",++kase,ans); 34 } 35 return 0; 36 }