Problem:
Packing Rectangles
IOI 95
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.
PROGRAM NAME: packrec
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.
SAMPLE INPUT (file packrec.in)
1 2
2 3
3 4
4 5
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.
SAMPLE OUTPUT (file packrec.out)
40
4 10
5 8
My Answer:
My Gain:
一开始发现题目给出的第四和第五种pack方式其实是一回事。
接着完全按照图上的四个矩形,编号i,j,k,l,边画图边写程序。
写好后发现,情况不完全。
后来在别人的博客里看到他使用了旋转,就想到,情况不完全无非是因为可以横着放也可以竖着放。
假如横的状态为1,竖的状态为0,很显然,四个矩形有2^4种情况,可由二进制进行演绎。
这样一来,作16次循环检验,就可以包括所有情况了,甚至还有重复。
另外需要注意的是,每次循环检验后,要记得还原四个矩形。