您的位置:首页 > 其它

动态规划 zoj1093 Monkey and Banana

2013-07-29 17:01 204 查看
Description

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 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 Specification

The input file will contain one or more test cases. The first line of each test case contains an 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 integers
representing 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

算是集训开始做的第一个动态规划题目,参考的白书 p161 的9.2.1节 例题9-2 嵌套矩形,不过在数据上要稍作处理。
现在简要的给自己分析一下这道题.
读入一个长方体的三维系数后,我们可以看出得到了3种带权值的矩形(长宽高相同也没什么影响),为什么这样分析呢,因为约束条件是两条边的大小。
大致分析:这是一个入口未知,出口待定(有入口结合最优解远离决定)的dp。状态带有权值。
功能分析:g[i][j],方便比较(i和j)的大小,当然,放到dp函数中也是可以的。注意这里的矩形是不需要按照顺序放置的。嘿嘿,我想到了,是不是我们也可以给一个预处理呢(但是排序的标准有是什么呢?好像没什么优化啊。。。)
d
:记忆化搜索的标记。
dp(){记忆化搜索+递归}
数据处理:保证x<y.不然就又要加一组(y,x,z).之前是(x,y,z)
我的不足:1、对递归和栈原理不清!!!!
2、学会用递推法计算
上代码咯:


#include<iostream>
#include<stdio.h>
#include<string.h>
#define maxn 300+5
using namespace std;
int x[maxn],y[maxn],v[maxn],d[maxn];
int g[maxn][maxn];
void pai(int *a1,int *a2,int *a3){
if (*a1>*a2){
int t;t=*a1;*a1=*a2;*a2=t;
}
if (*a1>*a3){
int t;t=*a1;*a1=*a3;*a3=t;
}
if (*a2>*a3){
int t;t=*a2;*a2=*a3;*a3=t;
}
return ;
}
int dp(int i,int n){
//    int& ans=d[i];
if (d[i]>0) return d[i];//防重复运算,记忆化搜索
d[i]=v[i];
for (int j=1;j<=3*n;j++){
if(g[j][i])//表示j能放在i号上
d[i]=max(d[i],dp(j,n)+v[i]);//直到不能再放的时候,递归调用才会结束啊!!!!
}
return ans;
}
int main(){
//freopen("test.in","r",stdin);
//freopen("test.out","w",stdout);
int n;
int t=1;
while(cin>>n){
if (n==0) break;
int a[4];
for (int i=0;i<n;i++){
for (int j=0;j<3;j++){//因为在长宽高互不相等的情况下,我们最大可能能得到3种组合
cin>>a[j];
}
pai(&a[0],&a[1],&a[2]);
x[i*3+1]=a[0],y[i*3+1]=a[1],v[i*3+1]=a[2];
x[i*3+2]=a[1],y[i*3+2]=a[2],v[i*3+2]=a[0];
x[i*3+3]=a[0],y[i*3+3]=a[2],v[i*3+3]=a[1];
}
memset(g,0,sizeof(g));
memset(d,0,sizeof(d));
for (int i=1;i<=3*n;i++){//3*n:现在最多已经有3n种砖块了
for (int j=1;j<=3*n;j++){
if (x[i]<x[j] && y[i]<y[j]) g[i][j]=1;//g[i][j]表示i可以放在j之上
}
}
int ans=0;
for (int i=1;i<=3*n;i++){//表示从每种砖块出发,能找到的最高存放方法
if(ans<dp(i,n)) ans=dp(i,n);
}
cout<<"Case "<<t<<": maximum height = "<<ans<<endl;
t++;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: