This is a C++ Program that Solves Box Stacking Problem using Dynamic Programming technique.
There are n cuboidal boxes. The dimensions of these boxes(length, breadth and height) are given. The objective is to stack the boxes to achieve maximum height. But you can place a box on top of another box only if its base dimensions are strictly lower than the other box. You can rotate the boxes. Also, you can use multiple instances of the same box.
This problem is a variation of Longest increasing subsequence. The dimensions of the boxes are given. First, generate all the dimensions(by rotating). Then sort all these dimensions based upon the base (l*b). Now, simply apply LIS technique on these dimensions.
n=3 dimensions(l,b,h) box 1 - (1,2,3) box 2 - (2,3,4) box 3 - (3,4,5) Expected result=15 Stacking from top to bottom 1,2,3 2,3,4 3,4,5 4,5,3
n=3 dimensions(l,b,h) box 1 - (1,2,3) box 2 - (2,3,4) box 3 - (4,5,6) Expected result=19 Stacking from top to bottom 1,2,3 2,3,4 3,4,2 4,5,6 5,6,4
Here is source code of the C++ Program to Solve Box Stacking Problem. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.
using namespace std;
//this structure contains the variables for the dimensions of a box ie, length, breadth and height
//comparison function used for sorting of all the boxes based on their base area
bool compare(box one, box two)
return one.l*one.b < two.l*two.b;
int boxStacking(vector<box> boxes, int n)
//vector to store all the results
//lis[i]=maximum attainable height by including box i at the bottom
//implementing linear increasing subsequence approach
//if dimensions of box j are lesser than box i
//box j can be placed on top of box i
if(boxes[j].l<boxes[i].l && boxes[j].b<boxes[i].b)
//now, choose the one which gives the maximum height
cout<<"Enter the number of boxes"<<endl;
//there are n boxes
//but a box can be rotated and can further give more boxes
//so, we will be storing dimensions of 3*n boxes by including the dimensions of rotated boxes as well
cout<<"Enter the dimensions of box (length,breadth,height) ";
//sort the boxes based on the base area (l*b)
cout<<"Longest achievable height by stacking the boxes is "<<endl;
In the main function, we ask the user to input the value for number of boxes and dimensions of each box. Then, we sort the ordering of these boxes with respect to the base area of the box. We pass these values to the function boxStacking as parameter. This function will calculate the expected result and return it. The returned value will be displayed.
Case-1: $ g++ box_stacking.cpp $ ./a.out Enter the number of boxes 3 Enter the dimensions of box (length,breadth,height) 1 2 3 Enter the dimensions of box (length,breadth,height) 2 3 4 Enter the dimensions of box (length,breadth,height) 4 5 6 Longest achievable height by stacking the boxes is 19
Sanfoundry Global Education & Learning Series – Dynamic Programming Problems.
To practice all Dynamic Programming Problems, here is complete set of 100+ Problems and Solutions.