您的位置:首页 > 其它

usaco 1.4.1 Packing Rectangles

2012-02-22 22:23 901 查看
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

进入新的章节后,发现题目赫然都不会做了……最讨厌模拟了好烦啊……不过……果然这个坎还是要自己跨过去吧!不要抄答案!试着自己写写吧!

不得不说在答案的帮助下这题还是解的比较顺的……就是最后一种的判断呆了,还有忘了清零。

/*
ID: wtff0411
PROG: packrec
LANG: C++
*/
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <vector>

using namespace std;

struct rr{
    int w,h;
}rec[4],recbig;

int tot=0;
int bigarea=9999999;
int res[100];
int numres;

int maxx(int x,int y)
{
    return x>y?x:y;
}
int minn(int x,int y)
{
    return x>y?y:x;
}

void record(int wid,int lon)
{
     int area=wid*lon;
     if(area>bigarea)
     return;
     if(area<bigarea)
     {
         bigarea=area;
         memset(res,0,sizeof(res));
         //when the minest area changed, the result before need to clear
     }
     res[min(wid,lon)]=1;
}

void check()
{
     int area;
     int lon,wid;
     //1.put together
     wid=rec[0].w+rec[1].w+rec[2].w+rec[3].w;
     lon=max(rec[0].h,rec[1].h);
     lon=max(lon,rec[2].h);
     lon=max(lon,rec[3].h);
     //area=wid*lon;
     record(wid,lon);
     //2.three put together, one at the bottom
     wid=rec[0].w+rec[1].w+rec[2].w;
     wid=max(wid,rec[3].h);
     lon=max(rec[0].h,rec[1].h);
     lon=max(lon,rec[2].h);
     lon+=rec[3].w;
     //area=lon*wid;
     record(wid,lon);
     //3.two together, one at the bottom, one aside
     lon=max(rec[0].h,rec[1].h);
     lon+=rec[2].w;
     lon=max(lon,rec[3].h);
     wid=max(rec[2].h,rec[0].w+rec[1].w);
     wid+=rec[3].w;
     record(wid,lon);
     //4 and 5.two together, two aside.
     lon=rec[0].h+rec[1].h;
     lon=max(lon,rec[2].h);
     lon=max(lon,rec[3].h);
     wid=max(rec[0].w,rec[1].w);
     wid+=(rec[2].w+rec[3].w);
     record(wid,lon);
     //6.a loop
     //02
     //13
     wid=rec[1].w+rec[3].w;
     lon=max(rec[0].h+rec[1].h,rec[2].h+rec[3].h);
     //0,2 conflict
     if(rec[2].w+rec[0].w>wid)
     wid=rec[2].w+rec[0].w;
     //0,3 conflict
     if((rec[1].h<rec[3].h)&&(rec[3].w+rec[0].w>wid))
     //think about the height.
     wid=rec[3].w+rec[0].w;
     //1,2 conflict
     if((rec[1].h>rec[3].h)&&(rec[2].w+rec[1].w>wid))
     wid=rec[2].w+rec[1].w;
     //0or 2 too wide
     wid=max(wid,rec[0].w);
     wid=max(wid,rec[2].w);
     record (wid,lon);
}
     
     
     

void ro(int n)
{
     int t=rec
.w;
     rec
.w=rec
.h;
     rec
.h=t;
}

void rotate(int n)
{
     if(n==4)
     {
         check();
         return;//don't forget to get back!!
     }
     ro(n);
     rotate(n+1);
     ro(n);
     rotate(n+1);
}
     
     

void solve(int n)
{
    if(n==4)
    rotate(0);
    for(int i=n;i<4;i++)
    {
        rr t=rec[i];
        rec[i]=rec
;
        rec
=t;
        solve(n+1);
        t=rec[i];
        rec[i]=rec
;
        rec
=t;
    }
}

int main()
{
    freopen("packrec.in","r",stdin);
    freopen("packrec.out","w",stdout);
    int i;
    memset(res,0,sizeof(res));
    for(i=0;i<4;i++)
    {
        cin>>rec[i].w>>rec[i].h;
        tot+=rec[i].w*rec[i].h;
    }
    solve(0);
    cout<<bigarea<<endl;
    for(i=1;i<100;i++)
    {
        if(res[i]!=0)
        cout<<i<<" "<<bigarea/i<<endl;
    }
    //system("pause");
    return 0;
}


而后面两题就是悲剧了……大悲剧啊!所谓不知道为什么错,于是乱改乱改……然后又不知道为什么改对了……然后又不知道为什么有的数据又通不过了……一笔糊涂账。头疼死了。不过总算今天也算做完两题了。sigh。




                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: