10

IOI 95

basic layouts

The six basic layouts of four rectangles

Four rectangles are given. Find the smallest enclosing (new) rectangle into which these four may be fitted without overlapping. By smallest rectangle, we mean the one with the smallest area.

All four rectangles should have their sides parallel to the corresponding sides of the enclosing rectangle. Figure 1 shows six ways to fit four rectangles together. These six are the only possible basic layouts, since any other layout can be obtained from a basic layout by rotation or reflection. Rectangles may be rotated 90 degrees during packing.

There may exist several different enclosing rectangles fulfilling the requirements, all with the same area. You must produce all such enclosing rectangles.

INPUT FORMAT
Four lines, each containing two positive space-separated integers that represent the lengths of a rectangle's two sides. Each side of a rectangle is at least 1 and at most 50.

OUTPUT FORMAT
The output file contains one line more than the number of solutions. The first line contains a single integer: the minimum area of the enclosing rectangles. Each of the following lines contains one solution described by two numbers p and q with p<=q. These lines must be sorted in ascending order of p, and must all be different.

So this is the problem statement. I figured out that I want to try all 24*16 positions ( you can turn rectangle 90 degrees) against all these basic layouts and check the new area, however I have no idea how to implement this. Anything from some pseudo code to links to articles would help a lot. Thanks in advance.

Bart Kiers
  • 166,582
  • 36
  • 299
  • 288
Marijus
  • 4,195
  • 15
  • 52
  • 87
  • 1
    It's not, in fact I'm still in high school and they're teaching loops there right now. – Marijus Mar 20 '11 at 20:06
  • Overlooked the IOI remark, sorry. – Doc Brown Mar 20 '11 at 20:11
  • 1
    Try googling for "constrained two dimensional cutting algorithm". I'm not sure there are really only 24*16 combinations. (though yours isn't exactly the classical cutting problem where the stock is usually fixed) – Guy Sirton Mar 20 '11 at 20:20
  • 1
    number of permutations: 4!, number of rotations: 2^4 – dcn Mar 20 '11 at 20:27
  • I don't know that you can just use linear permutations here. E.g. look at your right diagram: if the top right block was slightly smaller you could squeeze it in the gap between the other two. That is I don't think there is a 1:1 mapping between a linear order and a 2D arrangement. – Guy Sirton Mar 20 '11 at 20:40
  • related: [find smallest area that contains all the rectangles](http://stackoverflow.com/questions/12455392/find-smallest-area-that-contains-all-the-rectangles) – jfs Sep 17 '12 at 20:08

3 Answers3

5

While google does turn up some solutions, I guess some high level description will enable you to solve this on your own.

You can start by naming the rectangles in each of the 6 layout cases 1,2,3,4. Then you should be able to calculate the bounding box for each of the layouts for given instances of rectangles 1...4 (hint for the first case: width=sum of widths of 1...4, height=max of heigths of 1...4)

Then, as you said, you can try all possible combinations of naming four given rectangles with the indices 1...4 plus for each such possibility try out all possible rotations, and determine the minimum over all such possibilities in all layout cases.

dcn
  • 4,389
  • 2
  • 30
  • 39
2

Since there are only four rectangles, enumeration of all possible layouts should work. Of course really all possible layouts is too much, but we can merge equivalent layouts.

Let's consider only layouts where each rectangle cannot be moved left and up. This means that its upper border touches something and its left border touches something. All other layouts may be converted into such subset without making bounding box bigger.

When we choose the first rectangle (one of four), it can touch only left and top borders of bounding box, so we have only one possible position for its left-top vertex.

When we choose the second rectangle (one of three), its top border may touch either top border of the bounding box or bottom border of the first rectangle - 2 options. Similarly its left border may touch either left border of the bounding box or right border of the first rectangle - 2 more options. Thus we get 2x2=4 options for coordinate of its left-top vertex.

And so on for third and fourth rectangle.

Of course we should check at each step that there is no intersection between rectangles (e.g. if both of them will start from left-top corner of the bounding box). Also we sometimes will get obviously non-optimal layouts (e.g. when left-top vertex of the second rectangle coincides with right-bottom vertex of the first one), but that will not hurt result and can be removed if the solution is not fast enough.

maxim1000
  • 6,297
  • 1
  • 23
  • 19
0

basically all the 24*16 combination of the given rectangle can be derived from the above 6 diagrams shown by taking reflection and rotation into account and even the permutation of the rectangles

vivek
  • 1