I have a number of rectangles of various widths and heights. I have a larger rectangular platform to put them on. I want to pack them on one side of the platform so they spread in the lengthwise (X) dimension but keep the widthwise (Y) dimension to a minimal. That is to place them like a tetris game. There can be no overlaps but there can be gaps. Is there an algorithm out there to do this?
相关问题
- Finding k smallest elements in a min heap - worst-
- binary search tree path list
- High cost encryption but less cost decryption
- How to get a fixed number of evenly spaced points
- How to determine +/- sign when calculating diagona
相关文章
- What are the problems associated to Best First Sea
- Coin change DP solution to keep track of coins
- Algorithm for partially filling a polygonal mesh
- Robust polygon normal calculation
- Algorithm for maximizing coverage of rectangular a
- How to measure complexity of a string?
- Select unique/deduplication in SSE/AVX
- How to smooth the blocks of a 3D voxel world?
This is called 2D Strip Packing, and has been worked on by Martello. If you do a google search for their paper, their algorithm should be pretty easy to implement. One way to do it is to solve your problem using branch and bound. First compute a greedy solution to get a maximum height that your packing requires.
Your algorithm should then first find a set of x-coordinates that is promising, and then find the y-coordinates for your rectangles. In other words, for each rectangle, branch on all the possible x-coordinates you can assign it. At any point in time, you can keep a sum of the total height occupying any particular x-coordinate (this is called the cumulative constraint), and prune if the height exceeds your global maximum height. For every complete x-coordinate solution where all rectangles' x-coordinates have been assigned, you can now try to find valid y-coordinates. You can do this in the same way by branching, for every rectangle, on the different possible y-coordinates, pruning when you know two rectangles overlap each other. At the bottom of your tree you will have found both x and y-coordinates for your rectangles at which point you can compute the height required, and update your maximum upper bound.
If you have saved the current solution whenever you updated your upper bound, then when your algorithm terminates, you will have the optimal solution.
Sounds like a variation of Bin Packing:
A quote from the same page about possible solutions:
I suggest you follow some links from that Wikipedia page. Also, by Googling "bin packing algorithm" you'll probably find a lot of relevant information.