×

Box Stacking Problem

Stacking of boxes depending on their base

You have been given n different boxes. These boxes will have different heights, widths, and depths. You have to stack all these boxes in a manner such that the base surface area of upper box will not be greater than the base surface area of lower box. We can use different instances of a particular box.

Input:  {4, 6, 7}, {1, 2, 3}, {4, 5, 6}, {10, 12, 32}

Output:  60

Concept behind this solution

When solving the problem, we have to remember some points like it is not necessary to find small base by only width and depth. By rotating the box, we can also get the small base area. Another thing is that we can use different instances of a particular box. It means, we can use different rotations of the same box for building the stack. So, we have to first make an array which will consist of all possible rotations of all boxes. After that, we will sort the array by base area. Now we can use dp and tabulation approach. See the code for further understanding.

Code:

// C++ program to get maximum height of box stack

#include<stdio.h>
#include<stdlib.h>
/* Constructing box */
struct Newbx
{
int h, w, d; 
};
int min (int x, int y)
{ return (x < y)? x : y; }
int max (int x, int y)
{ return (x > y)? x : y; }
int compare (const void *a, const void * b)
{
	return ( (*(Newbx *)b).d * (*(Newbx *)b).w ) -
		( (*(Newbx *)a).d * (*(Newbx *)a).w );
}
int maxStackHeight( Newbx arr[], int n )
{
Newbx rot[3*n];
int index = 0;
for (int i = 0; i < n; i++)
{
	rot[index].h = arr[i].h;
	rot[index].d = max(arr[i].d, arr[i].w);
	rot[index].w = min(arr[i].d, arr[i].w);
	index++;
	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++;
	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++;
}
n = 3*n;
qsort (rot, n, sizeof(rot[0]), compare);
int msh[n];
for (int i = 0; i < n; i++ )
	msh[i] = rot[i].h;
for (int i = 1; i < n; i++ )
	for (int j = 0; j < i; j++ )
		if ( rot[i].w < rot[j].w &&
			rot[i].d < rot[j].d &&
			msh[i] < msh[j] + rot[i].h
			)
		{
			msh[i] = msh[j] + rot[i].h;
		}


int max = -1;
for ( int i = 0; i < n; i++ )
	if ( max < msh[i] )
		max = msh[i];


return max;
}
int main()
{
Newbx arr[] = { {4, 6, 7}, {1, 2, 3}, {4, 5, 6}, {10, 12, 32} };
int n = sizeof(arr)/sizeof(arr[0]);


printf("The maximum possible height of stack is %d\n",
		maxStackHeight (arr, n) );


return 0;
}

// JAVA program to get maximum height of box stack

import java.util.*;


public class bn {
	
	/* Constructing box */
	static class Newbx implements Comparable<Newbx>{
	
		
		int h, w, d, area;
		
		public Newbx(int h, int w, int d) {
			this.h = h;
			this.w = w;
			this.d = d;
		}
		
		@Override
		public int compareTo(Newbx o) {
			return o.area-this.area;
		}
	}
	static int maxStackHeight( Newbx arr[], int n){
		
		Newbx[] rot = new Newbx[n*3];
		
		for(int i = 0;i < n;i++){
			Newbx newbx = arr[i];
			
			rot[3*i] = new Newbx(newbx.h, Math.max(newbx.w,newbx.d),
									Math.min(newbx.w,newbx.d));
			
			rot[3*i + 1] = new Newbx(newbx.w, Math.max(newbx.h,newbx.d),
									Math.min(newbx.h,newbx.d));
			
			rot[3*i + 2] = new Newbx(newbx.d, Math.max(newbx.w,newbx.h),
									Math.min(newbx.w,newbx.h));
		}
		
		
		for(int i = 0; i < rot.length; i++)
			rot[i].area = rot[i].w * rot[i].d;
		
		Arrays.sort(rot);
		
		int count = 3 * n;
		
		indexes
		msh[i] --> Maximum possible Stack Height
				with newbx i on top */
		int[]msh = new int[count];
		for (int i = 0; i < count; i++ )
			msh[i] = rot[i].h;
		
		for(int i = 0; i < count; i++){
			msh[i] = 0;
			Newbx newbx = rot[i];
			int val = 0;
			
			for(int j = 0; j < i; j++){
				Newbx prevNewbx = rot[j];
				if(newbx.w < prevNewbx.w && newbx.d < prevNewbx.d){
					val = Math.max(val, msh[j]);
				}
			}
			msh[i] = val + newbx.h;
		}
		
		int max = -1;
		
		for(int i = 0; i < count; i++){
			max = Math.max(max, msh[i]);
		}
		
		return max;
	}
	public static void main(String[] args) {
		
		Newbx[] arr = new Newbx[4];
		arr[0] = new Newbx(4, 6, 7);
		arr[1] = new Newbx(1, 2, 3);
		arr[2] = new Newbx(4, 5, 6);
		arr[3] = new Newbx(10, 12, 32);
		
		System.out.println("The maximum possible "+
						"height of stack is " +
						maxStackHeight(arr,4));
	}
}

# Python program to get maximum height of box stack

class Newbx:
	
	# Constructing newbx
	def __init__(self, h, w, d):
		self.h = h
		self.w = w
		self.d = d


	def __lt__(self, other):
		return self.d * self.w < other.d * other.w


def maxStackHeight(arr, n):
	rot = [Newbx(0, 0, 0) for _ in range(3 * n)]
	index = 0


	for i in range(n):
		rot[index].h = arr[i].h
		rot[index].d = max(arr[i].d, arr[i].w)
		rot[index].w = min(arr[i].d, arr[i].w)
		index += 1
		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 += 1
		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 += 1
	n *= 3
	rot.sort(reverse = True)


	msh = [0] * n


	for i in range(n):
		msh[i] = rot[i].h
	for i in range(1, n):
		for j in range(0, i):
			if (rot[i].w < rot[j].w and
				rot[i].d < rot[j].d):
				if msh[i] < msh[j] + rot[i].h:
					msh[i] = msh[j] + rot[i].h


	maxm = -1
	for i in range(n):
		maxm = max(maxm, msh[i])


	return maxm
if __name__ == "__main__":
	arr = [Newbx(4, 6, 7), Newbx(1, 2, 3),
		Newbx(4, 5, 6), Newbx(10, 12, 32)]
	n = len(arr)
	print("The maximum possible height of stack is",
		maxStackHeight(arr, n))

Output: 

Box Stacking Problem

Related Topics

LCA of binary tree

Implementation //Writing a program to find the lowest common factor in a given binary search tree. #include <iostream> #include <vector> using namespace std; // the very first step is to create a binary tree. struct __nod { int...

8 minutes read.

Detect and Remove Loop in a Linked List

Create a function called detectAndRemovetheLoop() that verifies whether a given Linked List has a loop, eliminates the loop if it does, and returns true if it does. It returns false...

6 minutes read.

Array vs Linked List: Data Structure

Data structure: Difference Between Array and Linked List What is Array? An array is a linear data structure that can store similar data items for further processing. The similar data items...

3 minutes read.

Boruvkas algorithm

This algorithm is used for finding minimum spanning tree from a weighted graph. Like prim’s and kruskal’s algorithm it is also a greedy algorithm. Note:What is the minimum spanning tree?We know...

4 minutes read.

Types of Data Structures

Almost every programme or software system that has been built makes use of data structures. Furthermore, data structures are basics of computer science and software engineering. When it comes to...

7 minutes read.

Function to Insert a Node in a Binary Search Tree

Implementation // writing C++ code that will help us in implementing the insertion operation in a binary search tree. #include <bits/stdc++.h> using namespace std; // creating a new binary search tree node struct __nod { int...

8 minutes read.

Binary Tree to Doubly Linked List

Binary Tree to Doubly Linked List This article will explain how to convert the given binary tree into a Doubly Linked List. The left and right pointers in tree nodes are...

2 minutes read.

Interval Tree

Interval Tree Interval Tree: The concept is to increase a Binary Search Tree self-balancing such as Red Black Tree, and AVL Tree, so that every feature can be completed in time O(Logn). Each Interval...

4 minutes read.

Queue Implementation using stacks Data Structure

Queue Implementation using stacks In this problem, we have stack data structure which supports only push() and pop() operations. We are required to implement a queue data structure using the instances...

4 minutes read.

Given Two Binary Trees, Check if it is Symmetric

Implementation // creating a C++ program that will help us check whether the two given trees are mirror images of each other.  #include<bits/stdc++.h> using namespace std; /* A given binary tree has a data...

5 minutes read.

Traversal of binary tree

Traversal of binary tree: A node is visited only once in the traversal of the binary tree. There are three main types of traversal methods in the binary tree. In-order traversalPre-order...

3 minutes read.

Understanding Data Processing

Introduction Data In our everyday lives, any task that we perform online is related to data. Millions of pieces of data are produced every second across the globe. Data production is largely...

4 minutes read.

What is the Use of Segment Trees in Data Structure?

Segment trees Segment trees are also called statistical trees in computer science. They are a type of tree data structure. Segment trees are used to store information regarding segments and intervals....

6 minutes read.

Binary search tree traversal in-order pre-order post-order examples

A binary search tree is a type of non-linear tree in which the tree contains at least two nods. It is called binary because of its nature that states bi...

8 minutes read.

Treap data structure

In this article, we will discuss the treap data structure. The word treap is a combination of 'tree' and 'heap'. So, treap data structure is a combination of a heap...

8 minutes read.

Binary Search

Binary Search: When there is a large data structure, the linear search takes a lot of time to search the element. The binary search was developed to overcome the lack...

7 minutes read.

Common Operations on various Data Structures

Data structures are ways to organise data in computer memory for quick and effective use. The storage of data uses a variety of data-structures. It is also possible to define...

7 minutes read.

A Full Binary Tree with n Nodes

Implementation // Writing the implementation of the above approach in C++ #include <bits/stdc++.h> using namespace std; // We are creating a class that will create a node and its left and right children.  struct __nod...

12 minutes read.

Tree terminology in Data structures

Data structures The storage used to organize and store data is known as a data structure, and it is a method where data can be arranged on a computer to be...

6 minutes read.

Application of Stack in Data Structures

In this article, we will discuss all the different applications of stack. What is meant by stack? The stack is a non-primitive linear data structure in which the insertion of the new...

11 minutes read.