0

I understand the dynamic programming solution to the Box Stacking problem, which tries to find the maximum possible length of a stack that can be formed by a given set of boxes, that can be rotated in any direction, such that the lower box is always lesser in width and length as compared to a box that is higher in the stack.

However, I haven't been able to understand why only 3 orientations are required for every box. According to me, the number of orientations should be 6. That is, for each of the three dimensions taken as height, you should have two combinations.

An online resource shows the following as the method to create the three orientations(2 rotations) of the original box.

/* Create an array of all rotations of given boxes
      For example, for a box {1, 2, 3}, we consider three
      instances{{1, 2, 3}, {2, 1, 3}, {3, 1, 2}} */
   Box rot[3*n];
   int index = 0;
   for (int i = 0; i < n; i++)
   {
      // Copy the original box
      rot[index] = arr[i];
      index++;

      // First rotation of box
      rot[index].h = arr[i].w;
      rot[index].d = max(arr[i].h, arr[i].d);
      rot[index].w = min(arr[i].h, arr[i].d);
      index++;

      // Second rotation of box
      rot[index].h = arr[i].d;
      rot[index].d = max(arr[i].h, arr[i].w);
      rot[index].w = min(arr[i].h, arr[i].w);
      index++;
   }

So, For example, for a box {1, 2, 3}, the three orientations would be

1 2 3
2 1 3
3 1 2

But according to me, the orientations should be

1 2 3
1 3 2

2 1 3
2 3 1

3 1 2
3 2 1

I understand that my extra three combinations are due to alternating length and width between them keeping the height same. So, whereas I conside 1 2 3 and 1 3 2 to be different, the original algorithm considers them to be the sam.

However, I feel that, in this problem h,l,w and h,w,l, should be treated as two separate orientations since a box say, l=3,w=4,h=5 could be stacked above a box say, l=4,w=5,h=6, but not above a box l=5,w=4,h=6

tubby
  • 2,074
  • 3
  • 33
  • 55

1 Answers1

0

What you said is absolutely right we could take 6 orientations of the box, but the thing is we can make do with just 3 orientations of the box.

This is so because we impose the rule that given the height,width and length of a box, we shall treat the smaller of the two base dimensions as the width.

In other words, given a box with dimensions x > y > z, we'll consider orientations:

h=x, l=y, w=z
h=y, l=x, w=z
h=z, l=x, w=y

But not orientations such as:

h=x, l=z, w=y
h=y, l=z, w=x
h=z, l=y, w=x

This is merely to simplify the representation of the boxes.
Even in the link you have specified they have written,
for simplicity of solution, always keep w<=d:

struct Box
{
  // h –> height, w –> width, d –> depth
  int h, w, d;  // for simplicity of solution, always keep w <= d
};

Now that we have imposed this constraint on the data we can see that your original problem has been resolved i.e.

However, I feel that, in this problem h,l,w and h,w,l, should be treated as two separate orientations since a box say, l=3,w=4,h=5 could be stacked above a box say, l=4,w=5,h=6, but not above a box l=5,w=4,h=6

For example, if there were two boxes

h1,w1,l1 (Box1) and
h2,w2,l2 (Box2)

we know w1<l1(because we have imposed that restriction) so if by chance w1>w2 we know automatically l1> w2,and thus the other box configuration(where you said w and l should be swapped and considered as a separate configuration) is not required.

Just think about it, i am sure you'll understand why we use just 3 configurations.

Rahul Nori
  • 698
  • 6
  • 17