一 原题
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
二 分析
三 代码
USER: Qi Shen [maxkibb3] TASK: packrec LANG: C++ Compiling... Compile: OK Executing... Test 1: TEST OK [0.000 secs, 4184 KB] Test 2: TEST OK [0.000 secs, 4184 KB] Test 3: TEST OK [0.000 secs, 4184 KB] Test 4: TEST OK [0.000 secs, 4184 KB] Test 5: TEST OK [0.000 secs, 4184 KB] Test 6: TEST OK [0.000 secs, 4184 KB] Test 7: TEST OK [0.000 secs, 4184 KB] Test 8: TEST OK [0.000 secs, 4184 KB] Test 9: TEST OK [0.000 secs, 4184 KB] Test 10: TEST OK [0.000 secs, 4184 KB] Test 11: TEST OK [0.000 secs, 4184 KB] Test 12: TEST OK [0.000 secs, 4184 KB] Test 13: TEST OK [0.000 secs, 4184 KB] Test 14: TEST OK [0.000 secs, 4184 KB] Test 15: TEST OK [0.000 secs, 4184 KB] Test 16: TEST OK [0.000 secs, 4184 KB] Test 17: TEST OK [0.000 secs, 4184 KB] Test 18: TEST OK [0.000 secs, 4184 KB] Test 19: TEST OK [0.000 secs, 4184 KB] Test 20: TEST OK [0.000 secs, 4184 KB] Test 21: TEST OK [0.000 secs, 4184 KB] All tests OK.
Your program ('packrec') produced all correct answers! This is your submission #2 for this problem. Congratulations!
/*
ID:maxkibb3
LANG:C++
PROB:packrec
*/
#include<cstdio>
#include<set>
#include<algorithm>
int ans = 0x7fffffff;
struct Rec {
int x, y;
Rec() {}
Rec(int a, int b) {
if(a > b)
std::swap(a, b);
x = a, y = b;
}
bool operator < (const Rec &r) const {
return x < r.x;
}
}r[4];
std::set<Rec> s;
void update(int w, int h) {
if(w * h < ans) {
ans = w * h;
s.clear();
s.insert(Rec(w, h));
}
else if(w * h == ans)
s.insert(Rec(w, h));
}
void calc() {
int w = 0, h = 0, area;
// case 1
for(int i = 0; i < 4; i++) {
w += r[i].x;
h = std::max(h, r[i].y);
}
update(w, h);
// case 2
w = 0, h = 0;
for(int i = 0; i < 3; i++) {
w += r[i].x;
h = std::max(h, r[i].y);
}
w = std::max(w, r[3].x);
h += r[3].y;
update(w, h);
// case 3
w = std::max(r[0].x + r[1].x, r[2].x) + r[3].x;
h = std::max(r[3].y, r[2].y + std::max(r[0].y, r[1].y));
update(w, h);
// case 4
w = std::max(r[0].x, r[3].x) + r[1].x + r[2].x;
h = std::max(r[0].y + r[3].y, std::max(r[1].y, r[2].y));
update(w, h);
// case 5
w = std::max(r[0].x, r[1].x) + r[2].x + r[3].x;
h = std::max(r[0].y + r[1].y, std::max(r[2].y, r[3].y));
update(w, h);
// case 6
w = r[0].x + r[1].x;
h = std::max(r[0].y + r[2].y, r[1].y + r[3].y);
if(r[0].y < r[1].y)
w = std::max(w, r[2].x + r[1].x);
if(r[0].y + r[2].y > r[1].y)
w = std::max(w, r[2].x + r[3].x);
if(r[1].y < r[0].y)
w = std::max(w, r[0].x + r[3].x);
w = std::max(w, r[2].x);
w = std::max(w, r[3].x);
update(w, h);
}
void rotate(int idx) {
if(idx == 4) {
calc();
return;
}
rotate(idx + 1);
std::swap(r[idx].x, r[idx].y);
rotate(idx + 1);
std::swap(r[idx].x, r[idx].y);
}
void permutate(int idx) {
if(idx == 4) {
rotate(0);
return;
}
for(int i = idx; i < 4; i++) {
std::swap(r[i], r[idx]);
permutate(idx + 1);
std::swap(r[i], r[idx]);
}
}
int main() {
freopen("packrec.in", "r", stdin);
freopen("packrec.out", "w", stdout);
for(int i = 0; i < 4; i++)
scanf("%d%d", &r[i].x, &r[i].y);
permutate(0);
printf("%d\n", ans);
for(std::set<Rec>::iterator it = s.begin(); it != s.end(); it++)
printf("%d %d\n", it->x, it->y);
return 0;
}

本文介绍了一个经典的矩形打包问题,旨在寻找四个矩形能够放置的最小面积的新矩形。通过列举不同布局并考虑矩形旋转的情况,文章提供了一种有效的解决方案,并附带了完整的C++实现代码。
172

被折叠的 条评论
为什么被折叠?



