# Monkey and Banana (zoj 1093 动态规划)

188人阅读 评论(0)

Monkey and Banana

Time Limit: 2 Seconds      Memory Limit: 65536 KB

A group of researchers are designing an experiment to test the IQ of a monkey. They will hang a banana at the roof of a building, and at the mean time, provide the monkey with some blocks. If the monkey is clever enough, it shall be able to reach the banana by placing one block on the top another to build a tower and climb up to get its favorite food.

The researchers have n types of blocks, and an unlimited supply ofblocks of each type. Each type-i block was a rectangular solid withlinear 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 otherdimension 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 placedon top of another block as long as the two base dimensions of theupper block were both strictly smaller than the corresponding basedimensions 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 basescouldn'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 Specification

The input file will contain one or more test cases. The first line of each test case containsan integer n,
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 integersrepresenting the values xi, yi and zi.
Input is terminated by a value of zero (0) for n.

### Output Specification

For each test case, print one line containing the case number(they are numbered sequentially starting from 1) and the height of the tallest possible tower in the format"Case case: maximum height = height"

### Sample Input

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


### Sample Output

Case 1: maximum height = 40
Case 2: maximum height = 21
Case 3: maximum height = 28
Case 4: maximum height = 342


/*************************************************************************
> File Name: zoj1093_堆箱子.cpp
> Author: wust_lyf
> Mail: 2206478849@qq.com
> Created Time: 2015年11月21日 星期六 12时30分40秒
************************************************************************/

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<stack>
#define eps 1e-6
#define DBG printf("Hi\n")
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 105;
const int MAXN = 2005;

struct Box
{
int x,y,z;
}box[maxn];

int n;
int N;
int dp[maxn];

int cmp(Box a,Box b)
{
if (a.x==b.x)
return a.y>b.y;
return a.x>b.x;
}

int main()
{
int i,j;
int cas=0;
while (scanf("%d",&n),n)
{
N=1;
for (i=0;i<n;i++)
{
scanf("%d %d %d",&box[N].x,&box[N].y,&box[N].z);
if (box[N].x>box[N].y) swap(box[N].x,box[N].y);
N++;
box[N].x=box[N-1].x; box[N].y=box[N-1].z; box[N].z=box[N-1].y;
if (box[N].x>box[N].y) swap(box[N].x,box[N].y);
N++;
box[N].x=box[N-2].y; box[N].y=box[N-2].z; box[N].z=box[N-2].x;
if (box[N].x>box[N].y) swap(box[N].x,box[N].y);
N++;
}
N--;
sort(box+1,box+N+1,cmp);
memset(dp,0,sizeof(dp));
int ans=0;
for (i=1;i<=N;i++)
{
int Max=0;
dp[i]=box[i].z;
for (j=1;j<i;j++)
{
if (box[i].x<box[j].x&&box[i].y<box[j].y&&dp[j]>Max)
{
Max=dp[j];
}
}
dp[i]=max(dp[i],Max+box[i].z);
ans=max(ans,dp[i]);
}
printf("Case %d: maximum height = %d\n",++cas,ans);
}
return 0;
}


1
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：158982次
• 积分：5576
• 等级：
• 排名：第4878名
• 原创：261篇
• 转载：3篇
• 译文：0篇
• 评论：44条
阅读排行
最新评论