Main topic:
Given 4 rectangular blocks, find a minimum enclosing rectangle to put the 4 rectangular blocks in, but not overlap each other. The so-called minimum rectangle refers to the smallest rectangular area.
Ideas:
Enumeration of the order of the rectangle, and then according to the graph given by the topic can be judged, mainly to think of enumerations.
Code:
1#include <bits/stdc++.h>2 using namespacestd;3 inti,n,sum=10009, p[5],q[5],a[5],b[5];4 structData {intx, y; }ans[10000];5 BOOLused[5];6 7 BOOLCMP (data A,data b) {returna.x<b.x;}8 9 voidPdintLintR)Ten { One if(l*r<sum) A { -sum=l*r,ans[n=1].x=l,ans[1].y=R; - if(l>r) Swap (ans[1].x,ans[1].y); the return; - } - if(l*r==sum) - { +ans[++n].x=l,ans[n].y=R; - if(l>r) Swap (ANS[N].X,ANS[N].Y); + } A } at - voidDfsintk) - { - if(k>4) - { - intL=0, r=0, I; in for(i=1;i<5; i++) L=max (L,p[i]), r+=Q[i]; -PD (L,R);//1 to for(l=r=0, i=1;i<4; i++) L=max (L,p[i]), r+=Q[i]; +PD (l+p[4],max (r,q[4]));//2 -L=max (p[1],p[2]) +p[3],r=max (q[1]+q[2],q[3]); thePD (Max (l,p[4]), r+q[4]);//3 *L=max (p[1],p[2]), R=max (q[3],q[4]); $PD (l+p[3]+p[4],max (r,q[1]+q[2]));//5Panax NotoginsengL=max (p[1]+p[2],p[3]+p[4]); - if(q[1]>=q[2]) the if(q[4]>q[3]) PD (l,q[1]+q[4]); + Else A if(p[1]+p[3]<=L) PD (L,max (q[1]+q[4],q[2]+q[3]));//6 the return; + } - for(intI=1;i<5; i++) $ if(!Used[i]) $ { -used[i]=1; -P[k]=a[i],q[k]=b[i],dfs (k +1); theP[k]=b[i],q[k]=a[i],dfs (k +1); -used[i]=0;Wuyi } the } - Wu intMain () - { About for(i=1;i<5; i++) scanf ("%d%d",&a[i],&b[i]); $Dfs1), Sort (ans+1, ans+n+1, CMP), printf ("%d\n", sum); -printf"%d%d\n", ans[1].x,ans[1].y); - for(i=2; i<=n;i++) - if(ans[i].x!=ans[i-1].x) printf ("%d%d\n", ans[i].x,ans[i].y); A return 0; +}
[Usaco] to lay the rectangular block