-
Notifications
You must be signed in to change notification settings - Fork 62
/
Box_Stacking_Problem.cpp
61 lines (57 loc) · 1.35 KB
/
Box_Stacking_Problem.cpp
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
#include<bits/stdc++.h>
using namespace std;
struct Box{
int h;
int d;
int w;
};
bool comparator(Box b1, Box b2){
return (b1.d*b1.w)>(b2.d*b2.w);
}
int maxStackHeight(int h[],int d[],int w[],int n){
struct Box ini[n];
for(int i=0;i<n;i++){
ini[i].h=h[i];
ini[i].d=d[i];
ini[i].w=w[i];
}
struct Box b[n];
int ind=0;
for(int i=0;i<n;i++){
//Original
b[i].h = ini[i].h;
b[i].d = max(ini[i].d,ini[i].w);
b[i].w = min(ini[i].d,ini[i].w);
ind++;
//First Rotation
b[i].h = ini[i].d;
b[i].d = max(ini[i].h,ini[i].w);
b[i].w = min(ini[i].h,ini[i].w);
ind++;
//Second Rotation
b[i].h = ini[i].w;
b[i].d = max(ini[i].h,ini[i].d);
b[i].w = min(ini[i].h,ini[i].d);
ind++;
}
int n=3*n;
sort(b,b+n,comparator);
int msh[n];//maximum stack height array
for(int i=0;i<n;i++){
msh[i]=b[i].h;
}
for (int i = 1; i < n; i++ )
for (int j = 0; j < i; j++ )
if ( b[i].w < b[j].w &&
b[i].d < b[j].d &&
msh[i] < msh[j] + b[i].h
)
{
msh[i] = msh[j] + b[i].h;
}
int max = -1;
for ( int i = 0; i < n; i++ )
if ( max < msh[i] )
max = msh[i];
return max;
}