Box Stacking Problem

In this problem a set of different boxes are given, the length, breadth, and width may differ for different boxes. Our task is to find a stack of these boxes, whose height is as much as possible. We can rotate any box as we wish. But there is a rule to maintain.

One can place a box on another box if the area of the top surface for the bottom box is larger than the lower area of the top box.

Input and Output

Input:
A list of boxes is given. Each box is denoted by (length, bredth, height).
{ (4, 6, 7), (1, 2, 3), (4, 5, 6), (10, 12, 32) }
Output:
The maximum possible height of box stack is: 60

Algorithm

maxHeight(boxList, n)

Input − The list of different boxes, number of boxes.

Output − maximum height found by making the stacking of boxes.

Begin
   define rotation array rot of size 3n.
   index := 0

   for all boxes i, in the boxList, do
      rot[index].len := boxList[i].len
      rot[index].hei := maximum of boxList[i].hei and boxList[i].bre
      rot[index].bre := minimum of boxList[i].hei and boxList[i].bre
      index := index + 1

      rot[index].len := boxList[i].bre
      rot[index].hei := maximum of boxList[i].len and boxList[i].hei
      rot[index].bre := minimum of boxList[i].len and boxList[i].hei
      index := index + 1

      rot[index].len := boxList[i].hei
      rot[index].hei := maximum of boxList[i].len and boxList[i].bre
      rot[index].bre := minimum of boxList[i].len and boxList[i].bre
      index := index + 1

      n := 3n
      sort the rot list
      define maxHeightTemp array

      for i := 1 to n-1, do
         for j := 0 to i-1, do
            if rot[i].bre 

Example

#include
#include
using namespace std;

struct Box {
   int length, bredth, height;
};

int min (int x, int y) {
   return (x  y)? x : y;
}

bool compare(Box b1, Box b2) {
   return b1.height > b2.height;    //to sort the box as descending order of height
}

int maxHeight( Box boxList[], int n ) {
   Box rotation[3*n];    //a box can be rotared as 3 type, so there is 3n number of rotations
   int index = 0;

   for (int i = 0; i 

Output

The maximum possible height of box stack is: 60
Updated on: 2020-06-16T13:33:48+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements