Q9.10 To build the tallest stack
来源:互联网 发布:网件的访客网络 编辑:程序博客网 时间:2024/06/01 11:22
Q:You have a stack of n boxes, with widths w., heights l\ and depths dr The boxes cannot be rotated and can only be stacked on top of one another if each box in the stack is strictly larger than the box above it in width, height, and depth. Implement a method to build the tallest stack possible, where the heigh t of a stack is the sum of the heights of each box.
A: 给定箱子a,b,c,d,e...,那么结果为max(以a为底的栈,以b为底的栈,以c为底的栈,以d为底的栈,以e为底的栈...)。 子问题用同样的方法求解,只是用最底层的判断转为对倒数第二层的判断。为了节省时间,采用备忘录方法。
#include <iostream>#include <map>#include <vector>#include <cstdio>using namespace std;class box {private:float h, w, d;public:box(float H, float W, float D){h = H;w = W;d =D;}bool canBeAbove(box B);float getDepth();};bool box::canBeAbove(box B){if (h<B.h && w<B.w && d<B.d)return true;return false;}float box::getDepth(){return d;}typedef vector<box> vb;typedef map<int, vb> mvb;float stackHeight(vb Boxes){float height = 0;for (int i = 0; i < Boxes.size(); ++i) {height += Boxes[i].getDepth();}return height;}vb createStack(mvb &Map, vb Boxes, int bottom) {if (Map.count(bottom)!= 0 && bottom < Boxes.size()) {return Map[bottom];}int max_height = 0;vb max_stack;if (bottom >= Boxes.size()) {return max_stack;}for (int i = 0; i < Boxes.size(); ++i) {if (Boxes[i].canBeAbove(Boxes[bottom])) {vb tmpStack = createStack(Map, Boxes, i);int tmpHeight = stackHeight(tmpStack);if (tmpHeight > max_height) {max_stack = tmpStack;max_height = tmpHeight;}}}//Push Boxes[bottom] to font heemax_stack.push_back(Boxes[bottom]);Map[bottom] = max_stack;return max_stack;}int main(){freopen("Question9_10.in", "r", stdin);int n;cin>>n;mvb Map;vb Boxes, result;float H, W, D;for (int i = 0; i < n; ++i) {cin>>H>>W>>D;cout<<H<<" "<<W<<" "<<D<<endl;Boxes.push_back(box(H, W, D));}for (int i = 0; i < n; i++) {vb tmp = createStack(Map, Boxes, i);if (result.size() < tmp.size()) {result = tmp;}}for (int i = 0; i < result.size(); ++i) {cout<<result[i].getDepth()<<" ";}return 0;}
其中输入数据为:
4111333484222
0 0
- Q9.10 To build the tallest stack
- FZU 2077 the tallest trees
- Cracking the coding interview--Q9
- TOJ 4349: The Tallest in the Class
- Introduction to the Build Lifecycle
- How to build the webkitgtk
- Q9
- the globe's tallest store shopping
- Cracking the coding interview--Q9.7
- Cracking the coding interview--Q9.6
- Cracking the coding interview--Q9.3
- Cracking the coding interview--Q9.1
- Cracking the coding interview--Q9.2
- Cracking the coding interview--Q9.3
- Cracking the coding interview--Q9.4
- Cracking the coding interview--Q9.5
- Cracking the coding interview--Q9.6
- Cracking the coding interview--Q9.7
- Codeforces 508D Tanya and Password 欧拉通路Euler
- windows server2003与2008的区别总结
- Linux学习笔记
- Codeforces Beta Round #63 (Div. 2)
- HDU 1213 How Many Tables(kuangbin带你飞 专题五:并差集)
- Q9.10 To build the tallest stack
- ios 页面传值
- poj1845
- 检测 Linux 内存使用情况的 free 命令的10个例子
- 模型选择与特征选择
- Android 4.4 Graphic Architecture
- Hive严格模式
- 【Android】开发过程中值得注意的问题及解决方案
- java中volatile关键字的含义