hdu1069 Monkey and Banana--DP
发布日期:2021-10-03 20:31:52 浏览次数:6 分类:技术文章

本文共 4564 字,大约阅读时间需要 15 分钟。

原题链接:

一:原题内容

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
110 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 270
 
Sample Output
Case 1: maximum height = 40Case 2: maximum height = 21Case 3: maximum height = 28Case 4: maximum height = 342

二:分析理解

给出一些长方体,然后让你把他堆成塔,要求下面的塔的要比上面的塔大(长和宽),而且每一种长方体的数量都是无限的。

此题目考察到动态规划里的最长有序子序列。

三:AC代码

include
#include
#include
using namespace std;struct Node{ int length; int width; int height;};bool cmp(Node node1, Node node2){ if (node1.length > node2.length) return 1; if (node1.length == node2.length&&node1.width > node2.width) return 1; if (node1.length == node2.length&&node1.width == node2.width&&node1.height > node2.height) return 1; return 0;}Node node[200];int dp[200];int main(){ int n; int x, y, z; int icase = 1; int ans; while (scanf("%d", &n) && n) { int num = 0; for (int i = 0; i < n; i++) { scanf("%d%d%d", &x, &y, &z); //把给出的立方体放置的所有可能放进node[]中,这样就可以解决有无限块的问题 if (x == y) { if (y == z)//三个相等,放一个就够了 node[num].length = x, node[num].width = y, node[num++].height = z; else//x==y!=z时三种放法 { node[num].length = x, node[num].width = y, node[num++].height = z; node[num].length = x, node[num].width = z, node[num++].height = y; node[num].length = z, node[num].width = x, node[num++].height = y; } } else { if (y == z) { node[num].length = x, node[num].width = y, node[num++].height = z; node[num].length = y, node[num].width = x, node[num++].height = z; node[num].length = y, node[num].width = z, node[num++].height = x; } else if (x == z) { node[num].length = x, node[num].width = y, node[num++].height = z; node[num].length = x, node[num].width = z, node[num++].height = y; node[num].length = y, node[num].width = x, node[num++].height = z; } else//三个不等6种放法 { node[num].length = x, node[num].width = y, node[num++].height = z; node[num].length = x, node[num].width = z, node[num++].height = y; node[num].length = y, node[num].width = x, node[num++].height = z; node[num].length = y, node[num].width = z, node[num++].height = x; node[num].length = z, node[num].width = y, node[num++].height = x; node[num].length = z, node[num].width = x, node[num++].height = y; } } sort(node, node + num, cmp); dp[0] = node[0].height; ans = dp[0]; for (int j = 1; j < num; j++) { dp[j] = node[j].height; for (int k = 0; k < j; k++) { if (node[j].length < node[k].length&&node[j].width < node[k].width) dp[j] = max(dp[j], dp[k] + node[j].height); } if (ans < dp[j]) ans = dp[j]; } } printf("Case %d: maximum height = %d\n", icase++, ans); } return 0;}

转载地址:https://blog.csdn.net/LaoJiu_/article/details/50938401 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:hdu1176 免费馅饼--DP
下一篇:hdu2571 命运--DP

发表评论

最新留言

第一次来,支持一个
[***.219.124.196]2024年04月06日 12时28分25秒