hdu1069 Monkey and Banana--DP
发布日期:2021-10-03 20:31:52
浏览次数:5
分类:技术文章
本文共 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 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
感谢大佬
[***.8.128.20]2024年04月20日 09时30分53秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
PyPI的注册与模块发布
2019-04-27
Qt工作笔记-列表的分页显示(Qt Widgets框架)
2019-04-27
C++设计模式-模板方法模式
2019-04-27
Spring Boot笔记-发送消息给RabbitMQ
2019-04-27
强烈推荐 20 个免费和开源数据可视化工具
2019-04-27
“失败”的北漂十年,我真的尽力了。。。
2019-04-27
为什么使用 MD5 存储密码非常危险
2019-04-27
美团点评实时数仓实践
2019-04-27
Guava学习之Lists
2019-04-27
安装JUDDI服务器以及发布WSDL:第二部分,发布WSDL
2019-04-27
一文彻底搞懂令人疑惑的Java和JDK的版本命名!
2019-04-27
listview学习之simpleadapter详细介绍
2019-04-27
【Unity3D】 灯光学习
2019-04-27
Python3《机器学习实战》学习笔记(三):决策树实战篇之为自己配个隐形眼镜
2019-04-27
学生成绩信息管理c++
2019-04-27
Qt网络编程———TCP(1)
2019-04-27
java 构造方法中super()和this()
2019-04-27
华为HCIA-datacom 学习笔记汇总目录
2019-04-27
第4章 Python 数字图像处理(DIP) - 频率域滤波1 - 傅里叶级数和变换简史
2019-04-27