您的位置:首页 > 编程语言 > C语言/C++

USACO 1.4.1 Packing Rectangles

2012-07-17 12:56 337 查看
刚开始看到这题有点虚。。第6种情况实在是坑爹

看了些代码,最后决定舍弃别人的思路自己重新想,总算想清楚了,因为我取高度是max(左上+左下,右上+右下),宽度是max(左下+右下,左上+右上),所以重叠情况只能发生在左上和右下 或者 左下和右上。这样就清晰很多了。

代码实现我参考了nocow的第一个程序,那人写的真心漂亮。

一次AC,庆祝一下~!

/*
PROG:packrec
LANG:C++
*/

#include <cstdio>
#include <algorithm>
#include <cstring>

#define reset res_rec.x = res_rec.y = 0
using namespace std;

struct rectangle
{
int x, y;
}rec[4], res_rec;

int res = ~0U>>1;
bool exist[101];

void update()
{
if (res_rec.x * res_rec.y < res)
{
memset(exist, 0, sizeof(exist));
res = res_rec.x * res_rec.y;
}
if (res_rec.x * res_rec.y == res)
{
exist[min(res_rec.x, res_rec.y)] = 1;
}
}

void calc()
{
//case 1
reset;
for (int i = 0;i < 4;++i)
{
res_rec.x += rec[i].x;
res_rec.y = max(rec[i].y, res_rec.y);
}
update();

//case2
reset;
for (int i = 0;i < 3;++i)
{
res_rec.x += rec[i].x;
res_rec.y = max(rec[i].y, res_rec.y);
}
res_rec.x = max(res_rec.x, rec[3].x);
res_rec.y += rec[3].y;
update();

//case3
reset;
res_rec.x = max(rec[0].x+rec[1].x, rec[2].x)+rec[3].x;
res_rec.y = max(max(rec[0].y, rec[1].y)+rec[2].y, rec[3].y);
update();

//case4,5
reset;
res_rec.x = max(rec[0].x, rec[1].x)+rec[2].x+rec[3].x;
res_rec.y = max(max(rec[0].y+rec[1].y, rec[2].y), rec[3].y);
update();

//case6
reset;
res_rec.x = max(rec[0].x+rec[1].x, rec[2].x+rec[3].x);
res_rec.y = max(rec[0].y+rec[2].y, rec[1].y+rec[3].y);
if (rec[0].x>rec[2].x && rec[0].y>rec[1].y &&
rec[3].x>rec[1].x && rec[3].y>rec[2].y) res_rec.x = rec[0].x+rec[3].x;
if (rec[1].x>rec[3].x && rec[1].y>rec[0].y &&
rec[2].x>rec[0].x && rec[2].y>rec[3].y) res_rec.x = rec[1].x+rec[2].x;
update();
}

void rotate(int k)
{
if (k == 4) calc();
else
{
rotate(k+1);
swap(rec[k].x, rec[k].y);
rotate(k+1);
swap(rec[k].x, rec[k].y);
}
}

void dfs(int k)
{
if (k == 4) rotate(0);
else
for (int i = k;i < 4;++i)
{
swap(rec[i], rec[k]);
dfs(k+1);
swap(rec[i], rec[k]);
}
}
int main()
{
freopen("packrec.in", "r", stdin);
freopen("packrec.out", "w", stdout);
for (int i = 0;i < 4;++i) scanf("%d%d", &rec[i].x, &rec[i].y);
dfs(0);
printf("%d\n", res);
for (int i = 1;i <= 100;++i)
{
if (exist[i])
printf("%d %d\n", i, res/i);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c++