-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathBox Stacking.cpp
65 lines (63 loc) · 1.44 KB
/
Box Stacking.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
62
63
64
65
struct Box{
int l;
int b;
int h;
};
int min(int a,int b){
return (a<b)?a:b;
}
int max(int a,int b){
return (a>b)?a:b;
}
int compare (const void *a, const void * b) {
return ( (*(Box *)b).l * (*(Box *)b).b ) -
( (*(Box *)a).l * (*(Box *)a).b );
}
int solver(Box A[],int n){
Box B[3*n];
int index=0;
for(int i=0;i<n;i++){
B[index].h=A[i].h;
B[index].l=max(A[i].l , A[i].b );
B[index].b=min(A[i].l , A[i].b);
index++;
B[index].h=A[i].l;
B[index].l=max(A[i].h , A[i].b );
B[index].b=min(A[i].h , A[i].b);
index++;
B[index].h=A[i].b;
B[index].l=max(A[i].l , A[i].h );
B[index].b=min(A[i].l , A[i].h);
index++;
}
n = 3*n;
qsort (B, n, sizeof(B[0]), compare);
int msh[n];
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].l < B[j].l && B[i].b < B[j].b && 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;
}
int maxHeight(int height[],int breadth[],int length[],int n)
{
//Your code here
Box arr[n];
for(int i=0;i<n;i++){
arr[i].l=length[i];
arr[i].b=breadth[i];
arr[i].h=height[i];
}
return solver(arr,n);
}