I am having problem with code I am writing which is supposed to take a list of boxes of varying size, and pack them into a single large container box.
The main method which is supposed to implement a solution is called
fillContainerBox(). I have a basic nested if statement which will work for packing the first few boxes, but the code fails when it is necessary to unpack more than 1 box.
Please excuse my messy code. I have been trying to find a solution but have not found one.
Consider using a greedy algorithm.
You start with a box (let’s call it the package) that’s going to contain the other boxes. Of the boxes you need to put into the package, put the largest box in first (You could acheive this by sorting your list of boxes by volume in descending order, E.G. [largest box, 2nd largest, …, smallest box]) . Depending on the size of this box, the package might have some volume left over we can fill with other boxes. Now, in the remaining volumes we can try placing the second largest box following the same method used to fill the package. All we would have to do is partition the remaining space of the package and treat those partitions as smaller versions of the whole problem.