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.
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.
1 2 2 3 3 4 4 5
40 4 10 5 8
/* ID: qhn9992 PROG: packrec LANG: C++ */ #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <vector> using namespace std; const int INF=0x3f3f3f3f; typedef pair<int,int> pII; int rect[5][2]; int ANS[500][2],tot,MIN=INF; int a1,a2,a3,a4,b1,b2,b3,b4; void update(int weight,int height) { int area=weight*height; int mi=min(weight,height); int mx=max(weight,height); if(area>MIN) return ; else if(area<MIN) { tot=1; MIN=area; ANS[0][0]=mi;ANS[0][1]=mx; return ; } else { for(int i=0;i<tot;i++) { if(ANS[i][0]==mi&&ANS[i][1]==mx) return ; } ANS[tot][0]=mi;ANS[tot][1]=mx; tot++; } } void solve() { int wide,high; int w1=rect[a1][b1],w2=rect[a2][b2],w3=rect[a3][b3],w4=rect[a4][b4]; int h1=rect[a1][1-b1],h2=rect[a2][1-b2],h3=rect[a3][1-b3],h4=rect[a4][1-b4]; ///cas1 wide=w1+w2+w3+w4; high=max(max(h1,h2),max(h3,h4)); update(wide,high); ///cas2 wide=max(w4,w1+w2+w3); high=max(h1,max(h2,h3))+h4; update(wide,high); ///cas3 wide=w4+max(w1+w2,w3); high=max(h4,max(h1,h2)+h3); update(wide,high); ///cas4/5 wide=w1+w4+max(w2,w3); high=max(h1,max(h2+h3,h4)); update(wide,high); ///cas6 if(h2>=h4&&(w2<w1||w3<w4)) wide=max(w2+w4,w1+w3); else wide=max(w1,w2)+max(w3,w4); high=max(h1+h2,h3+h4); update(wide,high); } int main() { freopen("packrec.in","r",stdin); freopen("packrec.out","w",stdout); for(int i=0;i<4;i++) { int a,b; scanf("%d%d",&a,&b); rect[i][0]=a;rect[i][1]=b; } for(a1=0;a1<4;a1++) { for(a2=0;a2<4;a2++) { if(a1==a2) continue; for(a3=0;a3<4;a3++) { if(a3==a2||a3==a1) continue; a4=6-a1-a2-a3; for(b1=0;b1<2;b1++) for(b2=0;b2<2;b2++) for(b3=0;b3<2;b3++) for(b4=0;b4<2;b4++) solve(); } } } vector<pII> ans; for(int i=0;i<tot;i++) { int a=ANS[i][0],b=ANS[i][1]; ans.push_back(make_pair(a,b)); } sort(ans.begin(),ans.end()); printf("%d\n",MIN); for(int i=0;i<tot;i++) { printf("%d %d\n",ans[i].first,ans[i].second); } return 0; }