DP22 堆盒子问题 Box Stacking Problem @geeksforgeeks
2013-12-28 03:19
393 查看
You are given a set of n types of rectangular 3-D boxes, where the i^th box has height h(i), width w(i) and depth d(i) (all real numbers). You want to create a stack of boxes which is as tall as possible, but you can only stack
a box on top of another box if the dimensions of the 2-D base of the lower box are each strictly larger than those of the 2-D base of the higher box. Of course, you can rotate a box so that any side functions as its base. It is also allowable to use multiple
instances of the same type of box.
Source: http://people.csail.mit.edu/bdean/6.046/dp/. The link also has video for explanation of solution.
The Box Stacking problem is a variation of LIS problem. We need to build a maximum height stack.
Following are the key points to note in the problem statement:
1) A box can be placed on top of another box only if both width and depth of the upper placed box are smaller than width and depth of the lower box respectively.
2) We can rotate boxes. For example, if there is a box with dimensions {1x2x3} where 1 is height, 2×3 is base, then there can be three possibilities, {1x2x3}, {2x1x3} and {3x1x2}.
3) We can use multiple instances of boxes. What it means is, we can have two different rotations of a box as part of our maximum height stack.
Following is the solution based on DP solution of LIS problem.
1) Generate all 3 rotations of all boxes. The size of rotation array becomes 3 times the size of original array. For simplicity, we consider depth as always smaller than or equal to width.
2) Sort the above generated 3n boxes in decreasing order of base area.
3) After sorting the boxes, the problem is same as LIS with following optimal substructure property.
MSH(i) = Maximum possible Stack Height with box i at top of stack
MSH(i) = { Max ( MSH(j) ) + height(i) } where j < i and width(j) > width(i) and depth(j) > depth(i).
If there is no such j then MSH(i) = height(i)
4) To get overall maximum height, we return max(MSH(i)) where 0 < i < n
3维的就确定下两维(底面积),根据底面积排序,然后找高度的LDS
package DP;
import java.util.Arrays;
import java.util.Comparator;
public class BoxStacking {
public static void main(String[] args) {
Box[] A = new Box[4];
A[0] = new Box(4,6,7);
A[1] = new Box(1,2,3);
A[2] = new Box(4,5,6);
A[3] = new Box(10,12,32);
int n = A.length;
System.out.println(maxStackHeight(A, n));
}
// Time Complexity: O(n^2) Auxiliary Space: O(n)
public static int maxStackHeight(Box[] A, int n){
/* Create an array of all rotations of given boxes
For example, for a box {1, 2, 3}, we consider three
instances{{1, 2, 3}, {2, 1, 3}, {3, 1, 2}} */
Box[] rot = new Box[3*n];
int index = 0;
for(int i=0; i<n; i++){
rot[index] = A[i]; // Copy the original box
index++;
rot[index] = new Box(); // First rotation of box
rot[index].h = A[i].w;
rot[index].d = Math.max(A[i].h, A[i].d);
rot[index].w = Math.min(A[i].h, A[i].d);
index++;
rot[index] = new Box(); // Second rotation of box
rot[index].h = A[i].d;
rot[index].d = Math.max(A[i].h, A[i].w);
rot[index].w = Math.min(A[i].h, A[i].w);
index++;
}
n = 3*n; // Now the number of boxes is 3n
/* Sort the array ‘rot[]‘ in decreasing order, using library
function for quick sort */
Arrays.sort(rot, new Comparator<Box>() {
@Override
public int compare(Box o1, Box o2) { // 按底面积递减排序
return (o2.d*o2.w) - (o1.d*o1.w);
}
});
/* Initialize msh values for all indexes
msh[i] –> Maximum possible Stack Height with box i on top */
int[] msh = new int
;
for(int i=0; i<n; i++){
msh[i] = rot[i].h;
}
/* Compute optimized msh values in bottom up manner */
for(int i=1; i<n; i++){
for(int j=0; j<i; j++){
if(rot[i].w<rot[j].w && rot[i].d<rot[j].d){
msh[i] = Math.max(msh[i], msh[j]+rot[i].h);
}
}
}
/* Pick maximum of all msh values */
int max = 0;
for(int i=0; i<n; i++){
max = Math.max(max, msh[i]);
}
return max;
}
public static class Box{
int h, w, d; // h –> height, w –> width, d –> depth
// for simplicity of solution, always keep w <= d
public Box(){
}
public Box(int h_, int w_, int d_){
h = h_;
w = w_;
d = d_;
}
}
}
http://www.geeksforgeeks.org/dynamic-programming-set-21-box-stacking-problem/
http://people.csail.mit.edu/bdean/6.046/dp/
a box on top of another box if the dimensions of the 2-D base of the lower box are each strictly larger than those of the 2-D base of the higher box. Of course, you can rotate a box so that any side functions as its base. It is also allowable to use multiple
instances of the same type of box.
Source: http://people.csail.mit.edu/bdean/6.046/dp/. The link also has video for explanation of solution.
The Box Stacking problem is a variation of LIS problem. We need to build a maximum height stack.
Following are the key points to note in the problem statement:
1) A box can be placed on top of another box only if both width and depth of the upper placed box are smaller than width and depth of the lower box respectively.
2) We can rotate boxes. For example, if there is a box with dimensions {1x2x3} where 1 is height, 2×3 is base, then there can be three possibilities, {1x2x3}, {2x1x3} and {3x1x2}.
3) We can use multiple instances of boxes. What it means is, we can have two different rotations of a box as part of our maximum height stack.
Following is the solution based on DP solution of LIS problem.
1) Generate all 3 rotations of all boxes. The size of rotation array becomes 3 times the size of original array. For simplicity, we consider depth as always smaller than or equal to width.
2) Sort the above generated 3n boxes in decreasing order of base area.
3) After sorting the boxes, the problem is same as LIS with following optimal substructure property.
MSH(i) = Maximum possible Stack Height with box i at top of stack
MSH(i) = { Max ( MSH(j) ) + height(i) } where j < i and width(j) > width(i) and depth(j) > depth(i).
If there is no such j then MSH(i) = height(i)
4) To get overall maximum height, we return max(MSH(i)) where 0 < i < n
3维的就确定下两维(底面积),根据底面积排序,然后找高度的LDS
package DP;
import java.util.Arrays;
import java.util.Comparator;
public class BoxStacking {
public static void main(String[] args) {
Box[] A = new Box[4];
A[0] = new Box(4,6,7);
A[1] = new Box(1,2,3);
A[2] = new Box(4,5,6);
A[3] = new Box(10,12,32);
int n = A.length;
System.out.println(maxStackHeight(A, n));
}
// Time Complexity: O(n^2) Auxiliary Space: O(n)
public static int maxStackHeight(Box[] A, int n){
/* Create an array of all rotations of given boxes
For example, for a box {1, 2, 3}, we consider three
instances{{1, 2, 3}, {2, 1, 3}, {3, 1, 2}} */
Box[] rot = new Box[3*n];
int index = 0;
for(int i=0; i<n; i++){
rot[index] = A[i]; // Copy the original box
index++;
rot[index] = new Box(); // First rotation of box
rot[index].h = A[i].w;
rot[index].d = Math.max(A[i].h, A[i].d);
rot[index].w = Math.min(A[i].h, A[i].d);
index++;
rot[index] = new Box(); // Second rotation of box
rot[index].h = A[i].d;
rot[index].d = Math.max(A[i].h, A[i].w);
rot[index].w = Math.min(A[i].h, A[i].w);
index++;
}
n = 3*n; // Now the number of boxes is 3n
/* Sort the array ‘rot[]‘ in decreasing order, using library
function for quick sort */
Arrays.sort(rot, new Comparator<Box>() {
@Override
public int compare(Box o1, Box o2) { // 按底面积递减排序
return (o2.d*o2.w) - (o1.d*o1.w);
}
});
/* Initialize msh values for all indexes
msh[i] –> Maximum possible Stack Height with box i on top */
int[] msh = new int
;
for(int i=0; i<n; i++){
msh[i] = rot[i].h;
}
/* Compute optimized msh values in bottom up manner */
for(int i=1; i<n; i++){
for(int j=0; j<i; j++){
if(rot[i].w<rot[j].w && rot[i].d<rot[j].d){
msh[i] = Math.max(msh[i], msh[j]+rot[i].h);
}
}
}
/* Pick maximum of all msh values */
int max = 0;
for(int i=0; i<n; i++){
max = Math.max(max, msh[i]);
}
return max;
}
public static class Box{
int h, w, d; // h –> height, w –> width, d –> depth
// for simplicity of solution, always keep w <= d
public Box(){
}
public Box(int h_, int w_, int d_){
h = h_;
w = w_;
d = d_;
}
}
}
http://www.geeksforgeeks.org/dynamic-programming-set-21-box-stacking-problem/
http://people.csail.mit.edu/bdean/6.046/dp/
相关文章推荐
- 理解NSTextContainer
- bootstrap 和传统的html 关于button的属性问题
- GIT在Linux上的安装和使用简介
- 未来三年半,要和姑娘享受亲密吗?
- 给贺老师的一封邮件(问题是如何(HOW)去运用实践,去编程!!,去运用这些计算机科学与技术理论课程)
- stl string的substr和find_first_of函数的一点心得
- 将SVG转换为位图的方法(保持浏览器显示的样式)
- SRM 390(1-250pt)
- Apache Archiva: Maven服务端管理软件
- sql语句,查询时间段的数据
- CSS & HTML5 video switch
- SRM 391(1-250pt)
- linux shell 脚本攻略学习13--file命令详解,diff命令详解
- django之一
- DP21 LIS的变种问题-建桥问题 Variations of LIS-Building Bridge @geeksforgeeks
- Proxy实现java动态代理
- Order Authentic Cheap Jordan Gamma Blue 12s only $89
- iOS界面-仿网易新闻左侧抽屉式交互
- 闲情雅兴著文章
- IOS推送通知的实现步骤