Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6b1a903

Browse files
Box Stacking
1 parent 9f65c44 commit 6b1a903

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/* Amit Bansal - @amitbansal7 */
2+
#include <bits/stdc++.h>
3+
#define INF 0x3f3f3f3f
4+
using namespace std;
5+
// Box stacking.
6+
7+
struct Box
8+
{
9+
int l, w, h;
10+
};
11+
12+
bool compare(struct Box a, struct Box b)
13+
{
14+
return a.l * a.w > b.l * b.w;
15+
}
16+
17+
void CreateAllRoataions(int dimensions[][3], struct Box Boxes[], int n)
18+
{
19+
int index = 0;
20+
21+
for (int i = 0; i < n; i++)
22+
for (int l = 0; l < 3; l++)
23+
for (int w = 0; w < 3; w++)
24+
for (int h = 0; h < 3; h++)
25+
{
26+
if (h != l && w != l && w != h && dimensions[i][l] > dimensions[i][w])
27+
{
28+
Boxes[index].l = dimensions[i][l];
29+
Boxes[index].w = dimensions[i][w];
30+
Boxes[index].h = dimensions[i][h];
31+
index++;
32+
}
33+
}
34+
35+
}
36+
37+
int main()
38+
{
39+
const int n = 2;
40+
int dimensions[n][3] = {{1, 2, 4}, {3, 2, 5}};
41+
int t = 3*n;
42+
//t = Total number of possible boxes.
43+
struct Box Boxes[t];
44+
//Create all the combinations of boxes.
45+
CreateAllRoataions(dimensions, Boxes, 2);
46+
//Sort them in decreasing order according to l*h
47+
sort(Boxes, Boxes + t, compare);
48+
49+
int maxh[t];
50+
int result[t];
51+
52+
for(int i=0;i<t;i++)
53+
{
54+
maxh[i] = Boxes[i].h;
55+
result[i] = i;
56+
}
57+
58+
for(int i=1;i<t;i++)
59+
for(int j=0;j<i;j++)
60+
if(Boxes[i].l < Boxes[j].l && Boxes[i].w < Boxes[j].w)
61+
if(maxh[i] < maxh[j] + Boxes[i].h)
62+
{
63+
maxh[i] = maxh[j] + Boxes[i].h;
64+
result[i] = j;
65+
}
66+
67+
int idx;
68+
int maxheight = INT_MIN;
69+
70+
for(int i=0;i<t;i++)
71+
if(maxheight < maxh[i])
72+
{
73+
idx = i;
74+
maxheight = maxh[i];
75+
}
76+
77+
printf("Maximum height is %d\n",maxh[idx]);
78+
printf("Boxes used are \n");
79+
80+
while(result[idx] != idx)
81+
{
82+
printf("%d %d %d\n",Boxes[idx].l,Boxes[idx].w,Boxes[idx].h);
83+
idx = result[idx];
84+
}
85+
printf("%d %d %d\n",Boxes[idx].l,Boxes[idx].w,Boxes[idx].h);
86+
87+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /