Box Stacking

Box stacking: 

You are given a set of boxes {b1, ..., bn}. Each box bj has an associated width wj ,height hj and depth dj . Give an algorithm for creating the highest possible stack of boxes with theconstraint that if box bj is stacked on box bi, the 2D base of bi must be larger in both dimensionsthan the base of bj . You can of course, rotate the boxes to decide which face is the base, but youcan use each box only once.For example, given two boxes with h1 = 5, w1 = 5, d1 = 1 and h2 = 4, w2 = 5, h2 = 2, you shouldorient box 1 so that it has a base of 5x5 and a height of 1 and stack box 2 on top of it oriented sothat it has a height of 5 for a total stack height of 6.

Solution:Recursion: Memoize over H(j, R), the tallest stack of boxes with j on top with rotation R.

H(j, R) = 0 if j = 0

H(j, R) = max{(H(i, R) + hj )  with i <j && wi>wj && di>dj } if j > 0

Running Time: The size of H is O(n|R|) where R is the number of possible rotations for a box.For our purposes, |R| = 3 (since we only care about which dimension we designate as the “height”)so |H| = O(n). Filling in each element of H is also O(n) for a total running time of O(n2).


这个分析在处理每个 box 只能使用一次的解法可能是不对的,因为放盒子的顺序被限制了即后面的盒子能放到前面的盒子上面,反之却是不行的。


下面的每个盒子可以被使用多次的解法,这个是无疑义的

即把每个盒子按照三种 rotation 的方法拆成三个盒子,排序后再使用类似上面的 DP 解法即可。

http://www.geeksforgeeks.org/dynamic-programming-set-21-box-stacking-problem/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值