find smallest area that contains all the rectangles
This is an interview question.
We are given dimensions of various rectangles, we have to find out the area(minimum) of rectangle that can enclose all of them? rectangles can be rotated also .
test case:-
input:
3 //number of rectangles
8 8
4 3
3 4
output:
88
11x8:
+ - - - - - - + + - +
| | | |
| | | |
| | + - +
| | + - +
| | | |
| | | |
+ - - - - - - + + - +
i looked at a similar question asked before fitting rectangles in the smallest possible area
the above approach looks at all possibilities ,rotations, and determine the minimum over all such possibilities in all layout cases.
can't we base an algorithm in which we find the sum of area of rectangles first and then look for max length ,width?
这个问题没有绝对的解决方法,但有几个近似的解决方案,你可以在这里阅读其中的一些。
Optimal Rectangle Packing on Non-Square Benchmarks:
Given a set of rectangles, our problem is to find all enclosing rectangles of minimum area that will contain them without overlap. We refer to an enclosing rectangle as a bounding box. The optimization problem is NP-hard , while the problem of deciding whether a set of rectangles can be packed in a given bounding box is NP-complete , via a reduction from bin-packing (Korf 2003).
New Improvements in Optimal Rectangle Packing:
Korf [2003] divided the rectangle packing problem into two subproblems: the minimal bounding box problem and the containment problem. The former finds a bounding box of least area that can contain a given set of rectangles, while the latter tries to pack the given rectangles in a given bounding box. The algorithm that solves the minimal bounding box problem calls the algorithm that solves the containment problem as a subroutine.
Minimal Bounding Box Problem
A simple way to solve the minimal bounding box problem is to find the minimum and maximum areas that describe the set of feasible and potentially optimal bounding boxes. Bounding boxes of all dimensions can be generated with areas within this range, and then tested in non-decreasing order of area until all feasible solutions of smallest area are found. The minimum area is the sum of the areas of the given rectangles. The maximum area is determined by the bounding box of a greedy solution found by setting the bounding box height to that of the tallest rectangle, and then placing the rectangles in the first available position when scanning from left to right, and for each column scanning from bottom to top.
See also Optimal Rectangle Packing: New Results.
First of all you should check, could be enclosing rectangle be rotated or no? Anyway, you could ignore "rectangles" condition and resolve task in points. You have array of points (which are vertexes of rectangles). Your task is to find encosing rectangle with minimum area. If enclosing rectangle could not be rotated then solution is silly and has complexity O(n).
Generated array of rectangles and make array of points, which are vertexes of rectangles. Next is simple:
long n; // Number of vertexes
point arr[SIZE]; //Array of vertexes
long minX = MAXNUMBER, minY = MAXNUMBER, maxX = -MAXNUMER, maxY = -MAXNUMBER;
for (long i = 0; i < 4 * n; i++)
{
minX = MIN(minX, arr[i].x);
minY = MIN(minY, arr[i].y);
maxX = MIN(maxX, arr[i].x);
maxY = MIN(maxY, arr[i].y);
}
long width = maxX - minX, height = maxY - minY;
printf("%ddX%ld", width, height);
Another task if rectangle could be rotated. Then you should first:
Link for your task in wiki: http://en.wikipedia.org/wiki/Minimum_bounding_rectangle
链接地址: http://www.djcxy.com/p/63234.html上一篇: 如何找到覆盖另一个矩形的矩形区域
下一篇: 找到包含所有矩形的最小区域