Problem Description
A group of researchers aredesigning an experiment to test the IQ of a monkey. They will hang a banana atthe roof of a building, and at the mean time, provide the monkey with someblocks. If the monkey is clever enough, it shall be able to reach the banana byplacing one block on the top another to build a tower and climb up to get itsfavorite food.
The researchers have n types of blocks, and an unlimited supply of blocks ofeach type. Each type-i block was a rectangular solid with linear dimensions(xi, yi, zi). A block could be reoriented so that any two of its threedimensions determined the dimensions of the base and the other dimension wasthe height.
They want to make sure that the tallest tower possible by stacking blocks canreach the roof. The problem is that, in building a tower, one block could onlybe placed on top of another block as long as the two base dimensions of theupper block were both strictly smaller than the corresponding base dimensions ofthe lower block because there has to be some space for the monkey to step on.This meant, for example, that blocks oriented to have equal-sized basescouldn't be stacked.
Your job is to write a program that determines the height of the tallest towerthe monkey can build with a given set of blocks.
Input
The input file will containone 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. Themaximum value for n is 30.
Each of the next n lines contains three integers representing the values xi, yiand zi.
Input is terminated by a value of zero (0) for n.
Output
For each test case, print oneline containing the case number (they are numbered sequentially starting from1) 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
因为是上面的箱子是严格小于下面的箱子,所以,不存在一个箱子会无限使用的情况。可以把一个箱子看做三个箱子。x,y,z分别作为高。
转移方程:dp[i] = max(block[i],block[i]+dp[j]),表示i放在j上面(此处i和j满足严格小于的条件)
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
using namespace std;
typedef struct Blocks
{
int x ,y ,z;
};
Blocks block[31*3];
int num[3] ,dp[31*3];
int cmp(Blocks a ,Blocks b)
{
if(a.x==b.x)
{
if(a.y==b.y)
{
return a.z > b.z;
}
return a.y > b.y;
}
return a.x > b.x;
}
int main()
{
int n ,m ,cnt = 0;
while(scanf("%d",&n),n)
{
cnt++;
n = 3 * n;
for(int i = 0;i < n;)
{
scanf("%d%d%d",&num[0],&num[1],&num[2]);
block[i].x = max(num[0],num[1]);
block[i].y = min(num[0],num[1]);
block[i].z = num[2];
i++;
block[i].x = max(num[0],num[2]);
block[i].y = min(num[0],num[2]);
block[i].z = num[1];
i++;
block[i].x = max(num[1],num[2]);
block[i].y = min(num[1],num[2]);
block[i].z = num[0];
i++;
}
sort(block,block + n,cmp);
dp[0] = block[0].z;
for(int i = 0;i < n;i++)
{
m = 0;
for(int j = i - 1;j >= 0;j--)
{
if(block[i].x < block[j].x && block[i].y < block[j].y)
{
if(m < dp[j])
{
m = dp[j];
}
}
}
dp[i] = m + block[i].z;
}
m = 0;
for(int i = 0;i < n;i++)
{
if(dp[i] > m)
{
m = dp[i];
}
}
printf("Case %d: maximum height = %d\n",cnt,m);
}
return 0;
}