Monkey and Banana
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 13837 Accepted Submission(s): 7282
Problem Description
The researchers have n types of blocks, and an unlimited supply of blocks of each 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 three dimensions determined the dimensions of the base and the other dimension was the height.
They want to make sure that the tallest tower possible by stacking blocks can reach the roof. The problem is that, in building a tower, one block could only be placed on top of another block as long as the two base dimensions of the upper block were both strictly smaller than the corresponding base dimensions of the 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 bases couldn't be stacked.
Your job is to write a program that determines the height of the tallest tower the monkey can build with a given set of blocks.
Input
representing the number of different blocks in the following data set. The maximum value for n is 30.
Each of the next n lines contains three integers representing the values xi, yi and zi.
Input is terminated by a value of zero (0) for n.
Output
Sample Input
Sample Output
Source
//2017-03-14
#include <iostream>
#include <cstdio>
#include <cstring> using namespace std; const int N = ;
int n, dp[N*];//DAG模型,dp[i]表示从第i个箱子出发能够走的最大值
struct node
{
int x, y, z;
void setNode(int a, int b, int c){
this->x = a;
this->y = b;
this->z = c;
}
}box[N*]; int dfs(int i)
{
int& ans = dp[i];
if(ans)return ans;//记忆化搜索
ans = ;
for(int j = ; j < n*; j++)
{
if(box[i].x > box[j].x && box[i].y > box[j].y)
{
ans = max(ans, dfs(j));
}
}
ans += box[i].z;
return ans;
} int main()
{
int a, b, c, kase = ;
while(cin>>n && n)
{
int cnt = ;
memset(dp, , sizeof(dp));
for(int i = ; i < n; i++)
{
cin>>a>>b>>c;
box[cnt++].setNode(a, b, c);
box[cnt++].setNode(a, c, b);
box[cnt++].setNode(b, a, c);
box[cnt++].setNode(b, c, a);
box[cnt++].setNode(c, a, b);
box[cnt++].setNode(c, b, a);
}
for(int i = ; i < n*; i++)
dfs(i);
int ans = ;
for(int i = ; i < n*; i++)
if(dp[i] > ans)ans = dp[i];
cout<<"Case "<<++kase<<": maximum height = "<<ans<<endl;
} return ;
}