|
| 1 | +#include<stdio.h> |
| 2 | +#include<stdlib.h> |
| 3 | + |
| 4 | +struct Box |
| 5 | +{ |
| 6 | +int h, w, d; |
| 7 | +}; |
| 8 | + |
| 9 | +int min (int x, int y) |
| 10 | +{ return (x < y)? x : y; } |
| 11 | + |
| 12 | +int max (int x, int y) |
| 13 | +{ return (x > y)? x : y; } |
| 14 | + |
| 15 | +int compare (const void *a, const void * b) |
| 16 | +{ |
| 17 | + return ( (*(Box *)b).d * (*(Box *)b).w ) - |
| 18 | + ( (*(Box *)a).d * (*(Box *)a).w ); |
| 19 | +} |
| 20 | + |
| 21 | +int maxStackHeight( Box arr[], int n ) |
| 22 | +{ |
| 23 | +Box rot[3*n]; |
| 24 | +int index = 0; |
| 25 | +for (int i = 0; i < n; i++) |
| 26 | +{ |
| 27 | + rot[index].h = arr[i].h; |
| 28 | + rot[index].d = max(arr[i].d, arr[i].w); |
| 29 | + rot[index].w = min(arr[i].d, arr[i].w); |
| 30 | + index++; |
| 31 | + |
| 32 | + rot[index].h = arr[i].w; |
| 33 | + rot[index].d = max(arr[i].h, arr[i].d); |
| 34 | + rot[index].w = min(arr[i].h, arr[i].d); |
| 35 | + index++; |
| 36 | + |
| 37 | + rot[index].h = arr[i].d; |
| 38 | + rot[index].d = max(arr[i].h, arr[i].w); |
| 39 | + rot[index].w = min(arr[i].h, arr[i].w); |
| 40 | + index++; |
| 41 | +} |
| 42 | + |
| 43 | +n = 3*n; |
| 44 | + |
| 45 | +qsort (rot, n, sizeof(rot[0]), compare); |
| 46 | + |
| 47 | +int msh[n]; |
| 48 | +for (int i = 0; i < n; i++ ) |
| 49 | + msh[i] = rot[i].h; |
| 50 | + |
| 51 | +for (int i = 1; i < n; i++ ) |
| 52 | + for (int j = 0; j < i; j++ ) |
| 53 | + if ( rot[i].w < rot[j].w && |
| 54 | + rot[i].d < rot[j].d && |
| 55 | + msh[i] < msh[j] + rot[i].h |
| 56 | + ) |
| 57 | + { |
| 58 | + msh[i] = msh[j] + rot[i].h; |
| 59 | + } |
| 60 | + |
| 61 | + |
| 62 | +int max = -1; |
| 63 | +for ( int i = 0; i < n; i++ ) |
| 64 | + if ( max < msh[i] ) |
| 65 | + max = msh[i]; |
| 66 | + |
| 67 | +return max; |
| 68 | +} |
| 69 | + |
| 70 | +int main() |
| 71 | +{ |
| 72 | +Box arr[] = { {4, 6, 7}, {1, 2, 3}, {4, 5, 6}, {10, 12, 32} }; |
| 73 | +int n = sizeof(arr)/sizeof(arr[0]); |
| 74 | + |
| 75 | +printf("The maximum possible height of stack is %d\n", |
| 76 | + maxStackHeight (arr, n) ); |
| 77 | + |
| 78 | +return 0; |
| 79 | +} |
0 commit comments