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: xinming2
LANG: C++
TASK: packrec
*/
#include
#include
#include
#include
using namespace std;
int wx , hx;
vector > ww;
int wb[6] , hb[6];
int w[6] , h[6];
int calc1()
{
wb[1] = w[1] + w[2] + w[3] + w[4];
hb[1] = max(h[1] , max(h[2] , max(h[3] , h[4])));
return wb[1] * hb[1];
}
int calc2()
{
wb[2] = max(w[1] + w[2] + w[3] , w[4]);
hb[2] = max(h[1] , max(h[2] , h[3])) + h[4];
return wb[2] * hb[2];
}
int calc3()
{
wb[3] = max(w[1] + w[2] , w[3]) + w[4];
hb[3] = max(h[1] + h[3] , max(h[2] + h[3] , h[4]));
return wb[3] * hb[3];
}
int calc4()
{
wb[4] = max(w[3] , w[4]) + w[2] + w[1];
hb[4] = max(h[3] + h[4] , max(h[1] , h[2]));
return wb[4] * hb[4];
}
int calc5()
{
hb[5] = max(h[1] + h[3] , h[2] + h[4]);
if(h[3] >= h[2] + h[4])
{
wb[5] = max(w[1] , max(w[3] + w[2] , w[3] + w[4]));
}
else if(h[3] > h[4] && h[3] < h[2] + h[4])
{
wb[5] = max(w[1] + w[2] , max(w[2] + w[3] , w[3] + w[4]));
}
else if(h[4] > h[3] && h[4] < h[1] + h[3])
{
wb[5] = max(w[1] + w[2] , max(w[1] + w[4] , w[3] + w[4]));
}
else if(h[4] >= h[1] + h[3])
{
wb[5] = max(w[2] , max(w[1] + w[4] , w[3] + w[4]));
}
else
{
wb[5] = max(w[1] + w[2] , w[3] + w[4]);
}
return wb[5] * hb[5];
}
bool cmp(paira , pair b)
{
return a.first == b.first && a.second == b.second;
}
int main()
{
freopen("packrec.in" , "r" , stdin);
freopen("packrec.out" , "w" , stdout);
while(~scanf("%d%d" , &wx , &hx))
{
ww.clear();
ww.push_back(make_pair(wx , hx));
for(int i = 1 ; i < 4 ; i++)
{
scanf("%d%d" , &wx , &hx);
ww.push_back(make_pair(wx , hx));
}
vector > > > svec;
// int num = 0;
sort(ww.begin() , ww.end());//next_permutation以降序时的排列退出循环,所以一开始设为升序可以形成全排列!!!
do
{
for(int i1 = 0 ; i1 < 2 ; i1++)
{
for(int i2 = 0 ; i2 < 2 ; i2++)
{
for(int i3 = 0 ; i3 < 2 ; i3++)
{
for(int i4 = 0 ; i4 < 2 ; i4++)
{
if(i1)
{
w[1] = ww[0].first;
h[1] = ww[0].second;
}
else
{
w[1] = ww[0].second;
h[1] = ww[0].first;
}
if(i2)
{
w[2] = ww[1].first;
h[2] = ww[1].second;
}
else
{
w[2] = ww[1].second;
h[2] = ww[1].first;
}
if(i3)
{
w[3] = ww[2].first;
h[3] = ww[2].second;
}
else
{
w[3] = ww[2].second;
h[3] = ww[2].first;
}
if(i4)
{
w[4] = ww[3].first;
h[4] = ww[3].second;
}
else
{
w[4] = ww[3].second;
h[4] = ww[3].first;
}
int s1 = calc1();
int s2 = calc2();
int s3 = calc3();
int s4 = calc4();
int s5 = calc5();
int s = min(s1 , min(s2 , min(s3 ,min(s4 , s5))));
// for(int i = 1 ; i < 6 ; i++)cout << wb[i] << ' ' << hb[i] << endl;
vector > vec;
if(s == s1)
{
if(wb[1] > hb[1])vec.push_back(make_pair(hb[1] , wb[1]));
else vec.push_back(make_pair(wb[1] , hb[1]));
}
if(s == s2)
{
if(wb[2] > hb[2])vec.push_back(make_pair(hb[2] , wb[2]));
else vec.push_back(make_pair(wb[2] , hb[2]));
}
if(s == s3)
{
if(wb[3] > hb[3])vec.push_back(make_pair(hb[3] , wb[3]));
else vec.push_back(make_pair(wb[3] , hb[3]));
}
if(s == s4)
{
if(wb[4] > hb[4])vec.push_back(make_pair(hb[4] , wb[4]));
else vec.push_back(make_pair(wb[4] , hb[4]));
}
if(s == s5)
{
if(wb[5] > hb[5])vec.push_back(make_pair(hb[5] , wb[5]));
else vec.push_back(make_pair(wb[5] , hb[5]));
}
// sort(vec.begin() , vec.end());
// vector > :: iterator it;
// it = unique(vec.begin(), vec.end() , cmp);
// vec.reserve(distance(vec.begin() , it));
svec.push_back(make_pair(s , vec));
// cout << "s = " << s << ' ' << s1 << ' '<< s2 << ' '<< s3 << ' '<< s4 << ' ' << s5 << endl;
// num++;
}
}
}
}
// for(int i = 0 ; i < 4 ; i ++)
// {
// printf("%d = %d %d\n" , i + 1 , ww[i].first , ww[i].second);
// }
}while(next_permutation(ww.begin() , ww.end()));
// cout << "(((((((((((((((((((num)))))))))))))))))))" << num << endl;
sort(svec.begin() , svec.end());
// for(int i = 0 ; i < svec.size() ; i++)printf("**%d\n" , svec[i].first );
printf("%d\n" , svec[0].first);
//svec[i].second.size();
vector > ans;
for(int k = 0 ; k < svec.size() ; k++)
{
if(svec[k].first == svec[0].first)
{
for(int i = 0 ;i < svec[k].second.size() ; i++)
{
ans.push_back(make_pair(svec[k].second[i].first , svec[k].second[i].second));
}
}
}
// cout << "#$$$$" << ans.size() << endl;
sort(ans.begin() , ans.end());
vector > :: iterator it;
it = unique(ans.begin(), ans.end() , cmp);
ans.reserve(distance(ans.begin() , it));
int num = it - ans.begin();
ans.resize(num);
for(int i = 0 ; i < ans.size() ; i++)printf("%d %d\n" , ans[i].first , ans[i].second);
}
return 0;
}