Perhaps you have heard of the legend of the Tower of Babylon. Nowadays many details of this talehave been forgotten. So now, in line with the educational nature of this contest, we will tell you thewhole story:The babylonians hadntypes of blocks, and an unlimited supply of blocks of each type.Each type-iblock was a rectangular solid with linear dimensions(xi; yi; zi). A block couldbe reoriented so that any two of its three dimensions determined the dimensions of the baseand the other dimension was the height.They wanted to construct the tallest tower possible by stacking blocks. The problem wasthat, in building a tower, one block could only be placed on top of another block as long asthe two base dimensions of the upper block were both strictly smaller than the correspondingbase dimensions of the lower block. This meant, for example, that blocks oriented to haveequal-sized bases couldn’t be stacked.Your job is to write a program that determines the height of the tallest tower the babylonians canbuild with a given set of blocks.InputThe input file will contain one or more test cases. The first line of each test case contains an integern,representing the number of different blocks in the following data set. The maximum value fornis 30.Each of the nextnlines contains three integers representing the valuesxi,yiandzi.Input is terminated by a value of zero (0) forn.OutputFor each test case, print one line containing the case number (they are numbered sequentially startingfrom 1) and the height of the tallest possible tower in the format‘Casecase: maximum height =height’Sample Input110 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 270Sample OutputCase 1: maximum height = 40Case 2: maximum height = 21Case 3: maximum height = 28Case 4: maximum height = 342
看到矩形嵌套很容易想到建立dag图
那么我们只需要再DAG上找出一个权值最大的链
我们利用树形dp的思想在回溯是返回最大的链即可
/* ***********************************************
Author :CWb
Created Time :2019年10月24日 星期四 19时55分50秒
File Name :a.cpp
************************************************ */
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map>
using namespace std;
int len[105][10];
int dir[6][2]={2,3 ,3,2 , 1,3 ,3,1 ,1,2 ,2,1};
vector<int > G[605];
int dp[605];
int vis[605];
int dfs(int x)
{
//cout<<x<<endl;
if(dp[x]!=0) return dp[x];
int MAX=0;
for(auto v:G[x])
{
MAX=max(MAX,dfs(v));
}
dp[x]=MAX+len[x/6+1][(x%6+2)/2];
return dp[x];
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int n;
int cas=0;
while(~scanf("%d",&n))
{
if(n==0) return 0;
memset(vis,0,sizeof(vis));
memset(dp,0,sizeof(dp));
for(int i=0;i<n*6;i++) G[i].clear();
for(int i=1;i<=n;i++)
{
for(int j=1;j<=3;j++)
{
scanf("%d",&len[i][j]);
}
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
for(int l=0;l<6;l++)
{
for(int k=0;k<6;k++)
{
int tmp1=(i-1)*6+l;
int tmp2=(j-1)*6+k;
if(len[i][dir[l][0]]<len[j][dir[k][0]]&&len[i][dir[l][1]]<len[j][dir[k][1]])
{
G[tmp2].push_back(tmp1);
}
}
}
}
}
int ans=0;
for(int i=0;i<=n*6-1;i++)
{
ans=max(ans,dfs(i));
}
cas++;
printf("Case %d: maximum height = %d
",cas,ans);
}
return 0;
}
/*
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
*/