Software Development Exam  >  Software Development Notes  >  DSA in C++  >  Box Stacking Problem

Box Stacking Problem | DSA in C++ - Software Development PDF Download

Introduction

The box stacking problem is a classic Dynamic Programming problem that involves finding the maximum possible height that can be achieved by stacking boxes on top of each other. Each box has a width, height, and depth, and the goal is to stack the boxes in a way that maximizes the total height while ensuring that no box can be placed on top of another box with a smaller base area. In this article, we will explore the box stacking problem and provide a step-by-step approach to solve it using C++.

Overview of the Box Stacking Problem

The box stacking problem involves finding the maximum height that can be achieved by stacking boxes on top of each other. The following conditions must be satisfied while stacking the boxes:

  • The width, depth, and height of each box must be strictly smaller than those of the box below it.
  • Rotations of boxes are allowed. A box can be rotated to have a smaller base area, which may result in a greater height.

Approach to Solve the Problem

This doc is part of
153 videos|115 docs|24 tests
Join course for free

To solve the box stacking problem, we can follow these steps:

  • Generate all possible rotations for each box to cover all valid orientations.
  • Sort the boxes in non-increasing order of their base area.
  • Apply the Longest Increasing Subsequence (LIS) algorithm on the sorted boxes based on the box height.

Implementation in C++

Let's implement the box stacking problem using C++.

#include <iostream>

#include <algorithm>

#include <vector>

struct Box {

    int width, height, depth;

};

int maxStackHeight(std::vector<Box>& boxes) {

    std::vector<Box> rotations;   

    // Generate all possible rotations for each box

    for (const auto& box : boxes) {

        rotations.push_back(box);

        rotations.push_back({box.height, box.width, box.depth});

        rotations.push_back({box.depth, box.height, box.width});

    }   

    // Sort the boxes based on their base area in non-increasing order

    std::sort(rotations.begin(), rotations.end(), [](const Box& a, const Box& b) {

        return (a.width * a.depth) > (b.width * b.depth);

    });   

    int n = rotations.size();

    std::vector<int> dp(n, 0); // DP array to store maximum heights   

    // Apply the Longest Increasing Subsequence (LIS) algorithm

    for (int i = 0; i < n; i++) {

        dp[i] = rotations[i].height;

        for (int j = 0; j < i; j++) {

            if (rotations[j].width > rotations[i].width && rotations[j].depth > rotations[i].depth) {

                dp[i] = std::max(dp[i], dp[j] + rotations[i].height);

            }

        }

    }    

    // Find the maximum height

    int maxHeight = *std::max_element(dp.begin(), dp.end());    

    return maxHeight;

}

int main() {

    std::vector<Box> boxes = {

        {4, 6, 7},

        {1, 2, 3},

        {4, 5, 6},

        {10, 12, 32}

    };    

    int maxStackHeight = maxStackHeight(boxes);    

    std::cout << "Maximum stack height: " << maxStackHeight << std::endl;   

    return 0;

}

Download the notes
Box Stacking Problem
Download as PDF
Download as PDF

Examples and Code Explanation

Example 1: Basic Box Stacking

Let's consider a simple example with four boxes: Box 1 (4x6x7), Box 2 (1x2x3), Box 3 (4x5x6), and Box 4 (10x12x32).

Code Output:

Maximum stack height: 60

Code Explanation:

  • We generate all possible rotations for each box, resulting in a total of 12 rotations.
  • Sorting the boxes in non-increasing order of base area gives us the order: Box 4, Box 1, Box 3, and Box 2.
  • Applying the LIS algorithm, we find that the maximum stack height achievable is 60 units by stacking Box 4, Box 3, and Box 1.

Example 2: Handling Rotations

Let's consider another example with three boxes: Box 1 (1x2x3), Box 2 (2x3x4), and Box 3 (4x5x6).

Code Output:

Maximum stack height: 15

Code Explanation:

  • We generate all possible rotations for each box, resulting in a total of 9 rotations.
  • Sorting the boxes in non-increasing order of base area gives us the order: Box 3, Box 2, and Box 1.
  • Applying the LIS algorithm, we find that the maximum stack height achievable is 15 units by stacking Box 3 and Box 1.

Take a Practice Test
Test yourself on topics from Software Development exam
Practice Now
Practice Now

Sample Problems and Solutions

Problem 1: Given the following boxes, find the maximum stack height that can be achieved:

  • Box 1: 3x5x7
  • Box 2: 1x2x3
  • Box 3: 4x4x5
  • Box 4: 10x12x15

By applying the box stacking algorithm, we find that the maximum stack height achievable is 47 units by stacking Box 4, Box 3, and Box 1.

Problem 2: Given the following boxes, find the maximum stack height that can be achieved:

  • Box 1: 2x2x2
  • Box 2: 3x4x5
  • Box 3: 6x7x8
  • Box 4: 1x2x3

By applying the box stacking algorithm, we find that the maximum stack height achievable is 14 units by stacking Box 2 and Box 1.

Conclusion

In this article, we explored the box stacking problem and provided a step-by-step approach to solve it using C++. We implemented the solution and demonstrated its usage through examples. By understanding this problem and its solution, you can effectively tackle similar problems in the field of algorithmic problem-solving.

The document Box Stacking Problem | DSA in C++ - Software Development is a part of the Software Development Course DSA in C++.
All you need of Software Development at this link: Software Development
Are you preparing for Software Development Exam? Then you should check out the best video lectures, notes, free mock test series, crash course and much more provided by EduRev. You also get your detailed analysis and report cards along with 24x7 doubt solving for you to excel in Software Development exam. So join EduRev now and revolutionise the way you learn!
Sign up for Free Download App for Free
153 videos|115 docs|24 tests

Up next

153 videos|115 docs|24 tests
Download as PDF

Up next

Explore Courses for Software Development exam
Related Searches

Objective type Questions

,

Important questions

,

pdf

,

Previous Year Questions with Solutions

,

past year papers

,

Box Stacking Problem | DSA in C++ - Software Development

,

MCQs

,

Box Stacking Problem | DSA in C++ - Software Development

,

Summary

,

ppt

,

Exam

,

Free

,

Semester Notes

,

Box Stacking Problem | DSA in C++ - Software Development

,

mock tests for examination

,

Extra Questions

,

video lectures

,

practice quizzes

,

study material

,

Viva Questions

,

Sample Paper

,

shortcuts and tricks

;