1.4.1 Packing Rectangles

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

{
ID:makeeca1
PROG:packrec
LANG:PASCAL
}
var
  i,j,s,s0,s1,s2,s3,s4,x1,x2,x3,x4,y1,y2,y3,y4:longint;
  a,b,position:array[1..4]of longint;
  l:array[1..5,1..2]of longint;
  change:array[1..4] of boolean;
  matrix:array[1..200,1..200]of boolean;
procedure deal(n:longint);//用二进制模拟边的横竖情况
begin
    inc(position[n]);
    change[n]:=true;
    If (position[n]=2)and(n<4) then deal(n+1);
    position[n]:=position[n] mod 2
end;
procedure swap(Var a,b:longint);
begin
  a:=a xor b;
  b:=a xor b;
  a:=a xor b;
end;
function big(a,b:longint):longint;
begin If a>=b then exit(a) else exit(b);end;
begin
  assign(input,'packrec.in');  reset(input);
  assign(output,'packrec.out');  rewrite(output);
  for i:=1 to 4 do  read(a[i],b[i]);
  s0:=maxint;
  for i:=0 to 15 do
    begin
      fillchar(change,sizeof(change),0);
      deal(1);
      for j:=1 to 4 do If change[j] then swap(a[j],b[j]);
      for s1:=1 to 4 do
        for s2:=1 to 4 do
          if s2<>s1 then
            for s3:=1 to 4 do
              if (s3<>s1)and(s3<>s2)then
                begin
                  s4:=10-s1-s2-s3; 
                  x1:=a[s1]; y1:=b[s1];
                  x2:=a[s2]; y2:=b[s2];
                  x3:=a[s3]; y3:=b[s3];
                  x4:=a[s4]; y4:=b[s4];
                  l[1,1]:=x1+x2+x3+x4;  l[1,2]:=big(y1,big(y2,big(y3,y4)));
                  l[2,1]:=big(x1,x2+x3+x4);  l[2,2]:=y1+big(y2,big(y3,y4));
                  l[3,1]:=big(x1+x2,x3)+x4;  l[3,2]:=big(y4,big(y1,y2)+y3);
                  l[4,1]:=big(x2,x3)+x1+x4;  l[4,2]:=big(y1,big(y2+y3,y4));
                  l[5,1]:=big(x1+x2,big(x3,x4));  l[5,2]:=big(y1+y3,y2+y4);
                  if y1<y2 then l[5,1]:=big(l[5,1],x2+x3);
                  if y1>y2 then l[5,1]:=big(l[5,1],x1+x4);
                  if y1+y3>y2 then l[5,1]:=big(l[5,1],x3+x4); 
                  for j:=1 to 5 do
                    begin
                      if l[j,1]>l[j,2] then swap(l[j,1],l[j,2]);
                      s:=l[j,1]*l[j,2];
                      if s<s0 then
                        begin
                          fillchar(matrix,sizeof(matrix),0);
                          s0:=s
                        end;
                      if s=s0 then matrix[l[j,1],l[j,2]]:=true
                    end
                end
    end;
  writeln(s0);
  for i:=1 to 200 do
    for j:=i to 200 do
      if matrix[i,j] then writeln(i,' ',j); 
  close(output)
end.

posted on 2012-10-03 14:58  makeecat  阅读(325)  评论(0编辑  收藏  举报