Description
Perhaps you have heard of the legend of the Tower of Babylon. Nowadays many details of this tale has been forgotten. So now, on line with the educational nature of this contest, we'll 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 is a rectangular solid with linear dimensions. A Block could is reoriented so it three dimensions determined the dimensions of the base and the other Di Mension was the height. They wanted to construct the tallest tower possible by stacking blocks. The problem was, building a tower, one block could is placed on top of another block as long as the Dimensions of the upper block were both strictly smaller than the corresponding base dimensions of the lower block. This is meant, for example, which blocks oriented to has equal-sized bases couldn ' t be stacked.
Your job is to write a program this determines the height of the tallest tower the Babylonians can build with a given set of blocks.
Input and Output
The input file is contain one or more test cases. The first line of all test case contains a integer n, representing the number of different blocks in the follow ing 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 is numbered sequentially starting from 1) and the HEI Ght of the tallest possible tower in the format "case case: maximum height =height"
Sample Input
110 20 3026 8 105 5 571 1 12 2 23 3 34 4 45 5 56 6 67 7 7531 41 5926 53 5897 93 2384 62 6433 83 270
Sample Output
Case 1:maximum height = 40Case 2:maximum height = 21Case 3:maximum height = 28Case 4:maximum height = 342
Dynamic planning, each time one of the three sides of the enumeration cube is high, and the other side as long and wide, see if you can put down.
Need to remember
1 /*by Silvern*/2#include <iostream>3#include <algorithm>4#include <cstring>5#include <cstdio>6#include <cmath>7 using namespacestd;8 inte[ -][3];//three sides of the storage cube9 intf[ -][3];Ten intN,cnt=0; One voidPdintAint&b,int&c) { A Switch(a) { - Case 1: {b=0; c=2; Break;} - Case 2: {b=0; c=1; Break;} the Case 0: {b=1; c=2; Break;} - } - return; - } + intSolintKinth) { - if(F[k][h])returnF[K][H];//Memory of + inti,j; A intx1,y1; at PD (H,X1,Y1); - intX2,y2; - for(i=1; i<=n;i++) - for(j=0; j<=2; j + +) {//Enumeration High - PD (J,X2,Y2); - if((E[i][x2]>e[k][x1] && e[i][y2]>e[k][y1]) | | in(E[i][y2]>e[k][x1] && e[i][x2]>e[k][y1])) - { toF[k][h]=max (F[k][h],sol (i,j));//Recursive Solution + } - } thef[k][h]+=E[k][h]; * returnF[k][h]; $ }Panax Notoginseng intMain () { - inti,j; the intans; + while(SCANF ("%d", &n) &&N) { Aans=0; theMemset (F,0,sizeof(f)); + for(i=1; i<=n;i++) -scanf"%d%d%d", &e[i][0],&e[i][1],&e[i][2]); $ for(i=1; i<=n;i++) $ for(j=0; j<=2; j + +) -ans=Max (Ans,sol (i,j)); -printf"Case %d:maximum height =%d\n",++Cnt,ans); the } - return 0;Wuyi}
UVa 437 the Tower of Babylon