您的位置:首页 > 其它

USACO-cha1-sec1.4 Packing Rectangles

2014-07-03 14:06 417 查看
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

————————————————————归来的分割线————————————————————

前言:艰难的期末结束后,我以高数大物一起挂科的好(shi)成绩回到了ACM实验室。不想多说,这道题做了一整天还是不会,关键是不知道怎样枚举矩形摆放的状态。

思路:想要表示状态就要利用学习DFS时的一个知识点,用数字表示状态。

对于四个矩形摆在一起,有几种状态呢?根据题意,应该有6种,其实不然,题目给出的图4和图5是同一种情况。此外,还要考虑到一个特殊的情况。

假如反过来想,对某一特定情况取最小面积的时候,相当于在一个固定面积范围内摆放矩形,也就是切蛋糕。切三刀可以得到4块蛋糕,那么情况如下:



我们很自然的认为切三刀即可得到4个矩形,但是其实切四刀有可能得到一个更优的方案,尽管中间的蛋糕浪费了,但是这种时候浪费中间的比浪费四周的更好。如下:



如此一来 情况就齐了。用h和w来表示每个矩形的横向长度和纵向长度,计算出区域的面积。输出格式需要一定的技巧。如果面积比当前最小面积更小,则更新方案,如果更大则舍去,如果一样,则在该方案中增加一种情况。最后输出的时候进行一次排序。
代码如下:

/*
ID: j.sure.1
PROG: packrec
LANG: C++
*/
/****************************************/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <string>
#include <iostream>
using namespace std;
/****************************************/
int a[5][3], cas, pos[4], dir[4], minS = 2e9;
struct Node
{
int x, y;
}ans[100];

bool cmp(Node a, Node b)
{
return a.x < b.x;
}

void Judge(int a, int b)
{
int S = a * b;
if(S > minS)
return ;
if(S < minS) {
cas = 0;
minS = S;
}
if(a > b)
swap(a, b);
for(int i = 1; i <= cas; i++) {
if(a == ans[i].x && b == ans[i].y)
return ;
}
ans[++cas].x = a;
ans[cas].y = b;
}

void Work()
{
int h1 = a[pos[0]][dir[0]], h2 = a[pos[1]][dir[1]], h3 = a[pos[2]][dir[2]], h4 = a[pos[3]][dir[3]];
int w1 = a[pos[0]][3-dir[0]], w2 = a[pos[1]][3-dir[1]], w3 = a[pos[2]][3-dir[2]], w4 = a[pos[3]][3-dir[3]];
int width, height;
//情况1
width = w1 + w2 + w3 + w4;
height = max(max(max(h1, h2), h3), h4);///注意h和w一定是对应的
Judge(width, height);

//情况2
width = max(w4, w1 + w2 + w3);
height = max(max(h1, h2), h3) + h4;
Judge(width, height);

//情况3
width = w4 + max(w1 + w2, w3);
height  = max(h4, max(h1, h2) + h3);
Judge(width, height);

//情况4
width = w1 + w2 + max(w3, w4);
height = max(max(h1, h2), h3 + h4);
Judge(width, height);

//情况5
width = max(w1 + w2, w3 + w4);
height = max(h1, h2) + max(h3, h4);
Judge(width, height);

//情况6
if(w1 + w3 == w2 + w4 && h1 + h2 == h3 + h4)
if(h1 + h4 <= h1 + h2 && w2 + w3 <= w1 + w3){
width = w1 + w3;
height = h1 + h2;
Judge(width, height);
}
}

int main()
{
freopen("packrec.in", "r", stdin);
freopen("packrec.out", "w", stdout);
for(int i = 1; i < 5; i++) {
scanf("%d%d", &a[i][1], &a[i][2]);
}

for(pos[0] = 1; pos[0] < 5; pos[0]++) {
for(pos[1] = 1; pos[1] < 5; pos[1]++)
if(pos[1] != pos[0]) {
for(pos[2] = 1; pos[2] < 5; pos[2]++)
if(pos[2] != pos[1] && pos[2] != pos[0]) {
pos[3] = 10 - pos[0] - pos[1] - pos[2];
for(dir[0] = 1; dir[0] < 3; dir[0]++)
for(dir[1] = 1; dir[1] < 3; dir[1]++)
for(dir[2] = 1; dir[2] < 3; dir[2]++)
for(dir[3] = 1; dir[3] < 3; dir[3]++)
Work();
}
}
}
sort(ans+1, ans+cas+1, cmp);
printf("%d", minS);putchar('\n');
for(int i = 1; i <= cas; i++)
printf("%d %d\n", ans[i].x, ans[i].y);
fclose(stdin);
fclose(stdout);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: