Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8648925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:25:53+00:00 2026-06-12T13:25:53+00:00

You have a stack of n boxes, with widths wi, heights hi, and depths

  • 0

You have a stack of n boxes, with widths wi, heights hi, and depths
di. The boxes cannot be rotated and can only be stacked on top of one
another if each box in the stack larger than or equal to the box above
it in width, height, and depth. Implement a method to build the
tallest stack possible, where the height of a stack is the sum of the
heights of each box.

I know there are a couple of articles to talk about using dynamic programming to solve it. Since I’d like to do practice in writing recursion code, I wrote the following code:

const int not_possible = 999999;

class box{
public:
    int width;
    int depth;
    int height;
    box(int h=not_possible, int d=not_possible, int w=not_possible):
        width(w), depth(d), height(h) {}
};


bool check_legal(box lower, box upper){
    return (upper.depth<lower.depth) && 
           (upper.height<lower.height) &&
           (upper.width<lower.width);
}

void highest_stack(const vector<box>& boxes, bool* used, box cur_level, int num_boxes, int height, int& max_height)
{
    if(boxes.empty())
        return;

    bool no_suitable = true;
    for(int i = 0; i < num_boxes; ++i){
        box cur;
        if(!(*(used+i)) && check_legal(cur_level, boxes[i])){
            no_suitable = false;
            cur = boxes[i];
            *(used+i) = true;
            highest_stack(boxes, used, cur, num_boxes, height+cur.height, max_height);
            *(used+i) = false;

                    no_suitable = true;
        }
    }

    if(no_suitable){
        cout << height << endl; //for debug
        if(height > max_height)
            max_height = height;
            return;
    }
}

I’ve tested it using a lot of examples. For example:

boxes.push_back(box(4,12,32));
boxes.push_back(box(1,2,3));
boxes.push_back(box(2,5,6));
highest_stack(boxes, used, cur, boxes.size(), 0, max_height);

In the function highest_stack, there is one line cout << height << endl; for output. If I comment no_suitable = true;

the output is: 1 2 4; 1 2; 1, 1 4;

if I don’t comment no_suitable = true;

the output is: 1 2 4; 2 4; 4; 1 2; 2; 1; 1 4; 0

Both of them can give the correct result which is 7.

My question is:
(1) Can anyone help me verify my solution?
(2) Is there any more elegant recursive code for this problem? 

I don’t think my code is elegant.
Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T13:25:55+00:00Added an answer on June 12, 2026 at 1:25 pm

    I would make a directed graph where the nodes are boxes and the edges go from a box to a box that can be put on top of it. Then I’d use use the longest path algorithm to find the solution.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically in my website I have a sidebar with a stack of boxes; each
I have stack panel with custom controls in it. User can add or remove
I have view stack in a app like so: <mx:ViewStack id=viewStack left=0 right=0 top=0
As you can see http://jsfiddle.net/eZt4J/ I have a 6 boxes and I was wanting
I have a problem with a few div boxes displaying an overflowing/repeating image only
I have some text: A block of text is a stack of line boxes.
I have two select boxes like so: <select id=one> <option value=default>Select File</option> <option value=path/to/file1>File
I have a system that can output 0 or more select boxes and I
I have stack panel with custom controls in it. I attach standard MouseDragElementBehavior to
I have a stack panel for which AllowDrop property is true and I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.