用d[idx,k]来储存每个立方体,idx表示立方体的序号,k表示高,可以自定义一个结构体来储存长和宽。
#include<bits/stdc++.h>
using namespace std;
int height[35][3];
int dp[35][3];
int p[3];
struct retangle{
int a,b;
bool operator < (const retangle& rhs)const{
return ((a<rhs.a && b<rhs.b) ||(a<rhs.b && b<rhs.a));
}
}d[35][3];
int dp1(int i,int j,int n){
int &ans=dp[i][j];
if(ans) return ans;
ans=height[i][j];
for(int k=0;k<n;k++)
for(int p=0;p<3;p++)
if(d[i][j]<d[k][p] )
ans=max(ans,dp1(k,p,n)+height[i][j]);
return ans;
}
int main(){
int n,kase=1;
while(scanf("%d",&n) && n)
{
int ans1=0;
for(int i=0;i<n;i++)
{
int a,b,c;
scanf("%d %d %d",&p[0],&p[1],&p[2]);
sort(p,p+3);
d[i][0]={p[1],p[2]}; height[i][0]=p[0];
d[i][1]={p[0],p[2]}; height[i][1]=p[1];
d[i][2]={p[0],p[1]}; height[i][2]=p[2];
}
memset(dp,0,sizeof(dp));
for(int i=0;i<n;i++)
for(int j=0;j<3;j++)
ans1=max(ans1,dp1(i,j,n));
printf("Case %d: maximum height = %d\n",kase++,ans1);
}
}