USACO Section 1.4 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

题意:给你四个矩形,排放的的方式只有上面图中的六种情况,求一个完全包含上面四个矩形的最小矩形面积,并输出两条边。
分析:把四个矩形全排列,枚举每条边的摆放情况,所以总时间复杂度O(6*2^4*4!)。
View Code
  1 /* 
  2 ID: dizzy_l1
  3 LANG: C++
  4 TASK: packrec
  5 */
  6 #include<iostream>
  7 #include<algorithm>
  8 #include<cstdio>
  9 
 10 using namespace std;
 11 
 12 struct rectangles
 13 {
 14     int x,y;
 15 } r[4],tr[4];
 16 struct ANS
 17 {
 18     int x,y,area;
 19 } ans[3000];
 20 int k;
 21 
 22 void fun1(rectangles *A)
 23 {
 24     int i,a,b;
 25     a=b=0;
 26     for(i=0; i<4; i++)
 27     {
 28         a+=A[i].x;
 29         if(b<A[i].y)
 30             b=A[i].y;
 31     }
 32     if(a>b) swap(a,b);
 33     ans[k].x=a;
 34     ans[k].y=b;
 35     ans[k].area=a*b;
 36     ans[k].bug=1;
 37     k++;
 38 }
 39 
 40 void fun2(rectangles *A)
 41 {
 42     int a,b;
 43     a=max(A[0].y,A[1].x+A[2].x+A[3].x);
 44     b=A[0].x+max(A[1].y,max(A[2].y,A[3].y));
 45     if(a>b) swap(a,b);
 46     ans[k].x=a;
 47     ans[k].y=b;
 48     ans[k].area=a*b;
 49     ans[k].bug=2;
 50     k++;
 51 }
 52 
 53 void fun3(rectangles *A)
 54 {
 55     int a,b;
 56     a=A[1].x+max(A[0].y,A[2].x+A[3].x);
 57     b=A[0].x+max(A[2].y,A[3].y);
 58     b=max(b,A[1].y);
 59     if(a>b) swap(a,b);
 60     ans[k].x=a;
 61     ans[k].y=b;
 62     ans[k].area=a*b;
 63     ans[k].bug=3;
 64     k++;
 65 }
 66 
 67 void fun4(rectangles *A)
 68 {
 69     int a,b;
 70     a=A[0].x+A[1].x;
 71     a=a+max(A[2].x,A[3].x);
 72     b=A[2].y+A[3].y;
 73     b=max(b,max(A[0].y,A[1].y));
 74     if(a>b) swap(a,b);
 75     ans[k].x=a;
 76     ans[k].y=b;
 77     ans[k].area=a*b;
 78     ans[k].bug=4;
 79     k++;
 80 }
 81 
 82 void fun5(rectangles *A)
 83 {
 84     int a,b;
 85     a=max(A[0].y+A[3].y,A[1].y+A[2].y);
 86     if(A[0].y>=A[1].y&&A[2].x>A[1].x)
 87     {
 88         a=max(a,A[0].y+A[2].y);
 89     }
 90     if(A[0].y<=A[1].y&&A[3].x>A[0].x)
 91     {
 92         a=max(a,A[1].y+A[3].y);
 93     }
 94     b=max(A[0].x+A[1].x,A[2].x+A[3].x);
 95     if(a>b) swap(a,b);
 96     ans[k].x=a;
 97     ans[k].y=b;
 98     ans[k].area=a*b;
 99     ans[k].bug=5;
100     k++;
101 }
102 
103 bool cmp(ANS A,ANS B)
104 {
105     if(A.area<B.area) return true;
106     if(A.area==B.area&&A.x<B.x) return true;
107     if(A.area==B.area&&A.x==B.x&&A.y<B.y) return true;
108     return false;
109 }
110 
111 int main()
112 {
113     //freopen("packrec.in","r",stdin);
114     //freopen("packrec.out","w",stdout);
115     int i,a,b,c,d;
116     while(~scanf("%d%d%d%d%d%d%d%d",&r[0].x,&r[0].y,&r[1].x,&r[1].y,&r[2].x,&r[2].y,&r[3].x,&r[3].y))
117     {
118         int p[4];
119         for(i=0; i<4; i++)
120             p[i]=i;
121         do
122         {
123             for(i=0; i<4; i++)
124             {
125                 tr[i].x=r[p[i]].x;
126                 tr[i].y=r[p[i]].y;
127             }
128             for(a=0;a<2;a++)
129             {
130                 swap(tr[0].x,tr[0].y);
131                 for(b=0;b<2;b++)
132                 {
133                     swap(tr[1].x,tr[1].y);
134                     for(c=0;c<2;c++)
135                     {
136                         swap(tr[2].x,tr[2].y);
137                         for(d=0;d<2;d++)
138                         {
139                             swap(tr[3].x,tr[3].y);
140                             fun1(tr);fun2(tr);fun3(tr);
141                             fun4(tr);
142                             fun5(tr);
143                         }
144                     }
145                 }
146             }
147         }while(next_permutation(p,p+4));
148         sort(ans,ans+k,cmp);
149         int min_area=ans[0].area;
150         printf("%d\n",min_area);
151         printf("%d %d\n",ans[0].x,ans[0].y);
152         for(i=1;i<k;i++)
153         {
154             if(ans[i].area==min_area)
155             {
156                 if(ans[i].x==ans[i-1].x) continue;
157                 printf("%d %d\n",ans[i].x,ans[i].y);
158             }
159             else
160                 break;
161         }
162     }
163     return 0;
164 }

 





转载于:https://www.cnblogs.com/zhourongqing/archive/2012/08/27/2659046.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值