题目地址:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=6&problem=378&mosmsg=Submission+received+with+ID+17127418
思路:用2个维度表示状态转移,这个思路挺巧妙的
AC代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <cstring>
#include <climits>
#include <cmath>
#include <cctype>
const int inf = 0x3f3f3f3f;//1061109567
typedef long long LL;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
int value[35][3];//第一维表示立方体的序号,第二维表示立方体的长,宽,高
int dp1[35][3];
int n;
void get_arr(int* cf,int a,int b)
{
int k = 0;
for(int i=0; i<3; i++)
{
if(i != b)
cf[k++] = value[a][i];
}
}
int dp(int x,int y)//用i表示立方体的序号,j表示立方体的高度,来表示一种状态
{
int b[2],c[2];//这2个数组开成全局就错了
if(dp1[x][y] > 0)
return dp1[x][y];
int ans = 0;
get_arr(b,x,y);
for(int i=0; i<n; i++)
{
for(int j=0; j<3; j++)
{
get_arr(c,i,j);
if(b[0] > c[0] && b[1] > c[1])
ans = max(ans,dp(i,j));
}
}
ans += value[x][y];
dp1[x][y] = ans;
return ans;
}
int main()
{
int cas = 1;
while(scanf("%d",&n) && n)
{
memset(dp1,0,sizeof(dp1));
for(int i=0; i<n; i++)
{
for(int j=0; j<3; j++)
scanf("%d",&value[i][j]);
sort(value[i],value[i]+3);
}
int ans = 0;
for(int i=0; i<n; i++)//枚举每种立方体在最下面的情况
{
for(int j=0; j<3; j++)
ans = max(ans,dp(i,j));
}
printf("Case %d: maximum height = %d\n",cas++,ans);
}
return 0;
}