this is a simple 01 bags problem which need a little change of the number of goods. in this problem (link) we should stack our cuboid to the height given by problem,but we can’t determine the way of place the cuboid in equation . So we make one cuboid become three cuboids - the different ways of place them. Than we can solve this like normal 01 bags problem.
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 50000;
struct node
{
int x,y,h;
}block[510];
int dp[510];
int a[510];
int n = 0,len = 0;;
void place(int x,int y,int z)
{
len++;
block[len].x =x;
block[len].y = y;
block[len].h = z;
}
int cmp (node a,node b)
{
return a.x > b.x;
}
int main()
{
int cat = 1;
while(cin>>n)
{
if(!n) break;
len = 0;
int ans = 0;
int x,y,z;
for(int i = 1;i<=n;i++)
{
cin>>x>>y>>z;
place(x,y,z);
place(x,z,y);
place(y,x,z);
place(y,z,x);
place(z,y,x);
place(z,x,y);
}
sort(block+1,block+len+1,cmp);
memset(dp,0,sizeof(dp));
for(int i=1;i<=len;i++)
{
dp[i] = block[i].h;
for(int j = 1;j<i;j++)
{
if(block[i].x < block[j].x && block[i].y < block[j].y)
{
dp[i] = max(dp[i],dp[j] + block[i].h);
ans = max(ans,dp[i]);
}
}
}
cout<<"Case "<<cat++<<": maximum height = "<<ans<<endl;
}
return 0;
}