-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathBox Stacking Problem.java
119 lines (93 loc) · 2.56 KB
/
Box Stacking Problem.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/* Dynamic Programming implementation
of Box Stacking problem in Java*/
import java.util.*;
public class GFG {
/* Representation of a box */
static class Box implements Comparable<Box>{
// h --> height, w --> width,
// d --> depth
int h, w, d, area;
// for simplicity of solution,
// always keep w <= d
/*Constructor to initialise object*/
public Box(int h, int w, int d) {
this.h = h;
this.w = w;
this.d = d;
}
/*To sort the box array on the basis
of area in decreasing order of area */
@Override
public int compareTo(Box o) {
return o.area-this.area;
}
}
/* Returns the height of the tallest
stack that can be formed with give
type of boxes */
static int maxStackHeight( Box arr[], int n){
Box[] rot = new Box[n*3];
/* New Array of boxes is created -
considering all 3 possible rotations,
with width always greater than equal
to width */
for(int i = 0;i < n;i++){
Box box = arr[i];
/* Original Box*/
rot[3*i] = new Box(box.h, Math.max(box.w,box.d),
Math.min(box.w,box.d));
/* First rotation of box*/
rot[3*i + 1] = new Box(box.w, Math.max(box.h,box.d),
Math.min(box.h,box.d));
/* Second rotation of box*/
rot[3*i + 2] = new Box(box.d, Math.max(box.w,box.h),
Math.min(box.w,box.h));
}
/* Calculating base area of
each of the boxes.*/
for(int i = 0; i < rot.length; i++)
rot[i].area = rot[i].w * rot[i].d;
/* Sorting the Boxes on the bases
of Area in non Increasing order.*/
Arrays.sort(rot);
int count = 3 * n;
/* Initialize msh values for all
indexes
msh[i] --> Maximum possible Stack Height
with box i on top */
int[]msh = new int[count];
for (int i = 0; i < count; i++ )
msh[i] = rot[i].h;
/* Computing optimized msh[]
values in bottom up manner */
for(int i = 0; i < count; i++){
msh[i] = 0;
Box box = rot[i];
int val = 0;
for(int j = 0; j < i; j++){
Box prevBox = rot[j];
if(box.w < prevBox.w && box.d < prevBox.d){
val = Math.max(val, msh[j]);
}
}
msh[i] = val + box.h;
}
int max = -1;
/* Pick maximum of all msh values */
for(int i = 0; i < count; i++){
max = Math.max(max, msh[i]);
}
return max;
}
/* Driver program to test above function */
public static void main(String[] args) {
Box[] arr = new Box[4];
arr[0] = new Box(4, 6, 7);
arr[1] = new Box(1, 2, 3);
arr[2] = new Box(4, 5, 6);
arr[3] = new Box(10, 12, 32);
System.out.println("The maximum possible "+
"height of stack is " +
maxStackHeight(arr,4));
}
}