动态规划Problem-1069 Monkey and Banana

Posted aikoin

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了动态规划Problem-1069 Monkey and Banana相关的知识,希望对你有一定的参考价值。

动规题一直似懂非懂,今天做了道经典例题,加深理解。

下面给出原题

Problem 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
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
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
 
题目大意就是堆砖块,给定n种无限多的砖块,问最高能堆多高。
砖块有长宽高,规定上层砖块的长要严格小于下层砖块的长,上层砖块的宽也要严格小于下层砖块的宽。
思路:
从底层往顶层考虑。假设最底层放的是第i个砖块,那么可堆的最高高度就等于它上层可堆的最高高度加上砖块i本身的高度。
以此类推,一直到顶层,也就是砖块长和宽都最小的那层(它的上层不可再放置其它砖块了),以它为最底层,可堆的最高高度即为它本身高度。
状态转移方程:dp[i] = max{dp[j]} + block[i].h
 
下面给出AC代码,注释写的较详尽:
 1 #include <iostream>
 2 #include <algorithm>
 3 using namespace std;
 4 
 5 struct brick{
 6     int l=0,w=0,h=0;  //长宽高
 7 }b[1000]; //砖块结构体数组
 8 int dp[1000];  //dp[i]: 以第i个砖块为最底层可得到的最高高度
 9 
10 bool cmp(brick b1,brick b2){  //先长后宽从小到大排序
11     if(b1.l==b2.l) return b1.w<b2.w;
12     return b1.l<b2.l;
13 }
14 
15 int main()
16 {
17     int n;
18     int x,y,z;
19     int kase = 0;
20     while(cin>>n && n){
21         int len = 0;
22         for(int i = 0; i < n; ++i){
23             cin>>x>>y>>z;
24             //每个砖块有三种放置方法(考虑长>宽)
25             b[len].h = x,b[len].l = y>z?y:z,b[len++].w = y>z?z:y;
26             b[len].h = y,b[len].l = x>z?x:z,b[len++].w = x>z?z:x;
27             b[len].h = z,b[len].l = x>y?x:y,b[len++].w = x>y?y:x;
28         }
29         sort(b,b+len,cmp);  //以先长后宽从小到大的顺序给砖块排序
30         dp[0] = b[0].h;  //现在的b[0]是最小的砖块,上面不能放置其他砖块,高度为它本身
31         int max_h;
32         for(int i=1;i<len;++i){   //从第二个砖块开始循环,每次记录最大值 把第i个箱子放在前i-1个箱子的某一个下面
33             max_h = 0;  //前面0到i-1个砖块可以堆放的最高高度
34             for(int j=0;j<i;++j){
35                 if(b[j].l<b[i].l && b[j].w<b[i].w)
36                     max_h = max_h>dp[j]?max_h:dp[j];
37             }
38             dp[i] = b[i].h + max_h;  //以第i个砖块为最底层可得到的最高高度=前i-1层可堆放的最高高度+i本身高度
39         }
40         sort(dp,dp+len);
41         cout<<"Case "<<(++kase)<<": maximum height = "<<dp[len-1]<<endl;
42     }
43 
44     return 0;
45 
46 }

参考题解:

https://blog.csdn.net/little_dro/article/details/81590810

https://www.cnblogs.com/superxuezhazha/p/5792691.html

 









以上是关于动态规划Problem-1069 Monkey and Banana的主要内容,如果未能解决你的问题,请参考以下文章

动态规划第七届山东省省赛 Feed the monkey

动态规划第七届山东省省赛 Feed the monkey

动态规划第七届山东省省赛 Feed the monkey

动态规划第七届山东省省赛 Feed the monkey

python的猴子补丁monkey patch

问题 1069: 2005年春浙江省计算机等级考试二级C 编程题