Uva--437(动规,记忆化搜索)
2014-08-02 16:10:39
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
思路:经典的记忆化搜索类型题目,方块叠塔,由于一个方块可以旋转,我们可以通过把一个方块分身成三个来看(因为方块有三条对称轴,且每个方块不限量)。蒟蒻暂时还没想到地推关系式。。QAQ。
1 /************************************************************************* 2 > File Name: e.cpp 3 > Author: Nature 4 > Mail: 564374850@qq.com 5 > Created Time: Wed 30 Jul 2014 03:15:30 PM CST 6 ************************************************************************/ 7 8 #include <cstdio> 9 #include <cstring> 10 #include <cstdlib> 11 #include <cmath> 12 #include <iostream> 13 #include <algorithm> 14 using namespace std; 15 16 struct node{ 17 int x,y,z; 18 int cnt; 19 int g[105]; 20 }no[105]; 21 22 int n,ans[105]; 23 24 int Solve(int p){ 25 if(ans[p]) 26 return ans[p]; 27 int tmax = 0; 28 for(int i = 0; i < no[p].cnt; ++i){ 29 tmax = max(tmax,Solve(no[p].g[i])); 30 } 31 return (ans[p] = tmax + no[p].z); 32 } 33 34 void Build_graph(){ 35 for(int i = 0; i < n; ++i) 36 for(int j = 0; j < n; ++j) 37 if(no[i].x > no[j].x && no[i].y > no[j].y) 38 no[i].g[no[i].cnt++] = j; 39 } 40 41 int main(){ 42 int v[3]; 43 int Case = 0; 44 while(scanf("%d",&n) == 1 && n){ 45 memset(no,0,sizeof(no)); 46 memset(ans,0,sizeof(ans)); 47 for(int i = 0; i < n; ++i){ 48 scanf("%d%d%d",&v[0],&v[1],&v[2]); 49 sort(v,v + 3); 50 no[i * 3].x = v[0]; 51 no[i * 3].y = v[1]; 52 no[i * 3].z = v[2]; 53 54 no[i * 3 + 1].x = v[0]; 55 no[i * 3 + 1].y = v[2]; 56 no[i * 3 + 1].z = v[1]; 57 58 no[i * 3 + 2].x = v[1]; 59 no[i * 3 + 2].y = v[2]; 60 no[i * 3 + 2].z = v[0]; 61 } 62 n *= 3; 63 Build_graph(); 64 int tmax = 0; 65 for(int i = 0; i < n; ++i) 66 tmax = max(tmax,Solve(i)); 67 printf("Case %d: maximum height = %d\n",++Case,tmax); 68 } 69 return 0; 70 }