[搜索]USACO-1.4-Packing Rectangles

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


至于如何确定搜索策略,还是看了解题报告才知道了,题目有点阴,也怪自己没仔细看,原来总共的摆放方式已经有图给出了,那么直接把四个矩形往上面填就可以了,用DFS实现,类似于求全排列,不过最后一种摆放方式要注意处理,计算盒子面积时要分情况讨论一下就好了。

代码:

/*
ID:yfr_1992
PROG:packrec
LANG:C++
*/
#include<iostream>
#include<cstdio>
#include<vector>
#include<cstring>
#include<algorithm>
using namespace std;
struct Rectangle{
    int w,h;
}recs[8],temp[4];
vector< pair<int,int> > res;
bool vis[8],hash[500][500];
int minarea = 0x3fffffff;
void read(){
    for(int i=0;i<8;i+=2){
        scanf("%d %d",&recs[i].w,&recs[i].h);
        recs[i+1].w = recs[i].h , recs[i+1].h = recs[i].w;
    }
}
void dfs(int cur){
    if(cur==4){
        /*for(int i=0;i<cur;i++){
            printf("%d %d\n",temp[i].w,temp[i].h);
        }
        system("pause");*/
        int w,h;
case1:
        w = temp[0].w+temp[1].w+temp[2].w+temp[3].w;
        h = max(temp[0].h,temp[1].h);
        h = max(h,temp[2].h);
        h = max(h,temp[3].h);
        if(w*h<minarea){
            res.clear();
            minarea = w*h;
            if(!hash[min(w,h)][max(w,h)])
            res.push_back(make_pair(min(w,h),max(w,h))),hash[min(w,h)][max(w,h)]=1;
        }else if(w*h==minarea){
            if(!hash[min(w,h)][max(w,h)])
            res.push_back(make_pair(min(w,h),max(w,h))),hash[min(w,h)][max(w,h)]=1;
        }
case2:
        w = max(temp[0].w,temp[1].w+temp[2].w+temp[3].w);
        h = max(temp[1].h,temp[2].h);
        h = max(temp[3].h,h)+temp[0].h;
        if(w*h<minarea){
            res.clear();
            minarea = w*h;
            if(!hash[min(w,h)][max(w,h)])
            res.push_back(make_pair(min(w,h),max(w,h))),hash[min(w,h)][max(w,h)]=1;
        }else if(w*h==minarea){
            if(!hash[min(w,h)][max(w,h)])
            res.push_back(make_pair(min(w,h),max(w,h))),hash[min(w,h)][max(w,h)]=1;
        }
case3:
        w = max(temp[0].w,temp[1].w+temp[2].w)+temp[3].w;
        h = max(max(temp[1].h,temp[2].h)+temp[0].h,temp[3].h);
        if(w*h<minarea){
            res.clear();
            minarea = w*h;
            if(!hash[min(w,h)][max(w,h)])
            res.push_back(make_pair(min(w,h),max(w,h))),hash[min(w,h)][max(w,h)]=1;
        }else if(w*h==minarea){
            if(!hash[min(w,h)][max(w,h)])
            res.push_back(make_pair(min(w,h),max(w,h))),hash[min(w,h)][max(w,h)]=1;
        }
case4:
        w = temp[0].w+temp[3].w+max(temp[1].w,temp[2].w);
        h = max(max(temp[0].h,temp[3].h),temp[1].h+temp[2].h);
        if(w*h<minarea){
            res.clear();
            minarea = w*h;
            if(!hash[min(w,h)][max(w,h)])
            res.push_back(make_pair(min(w,h),max(w,h))),hash[min(w,h)][max(w,h)]=1;
        }else if(w*h==minarea){
            if(!hash[min(w,h)][max(w,h)])
            res.push_back(make_pair(min(w,h),max(w,h))),hash[min(w,h)][max(w,h)]=1;
        }
case5:
        w = max(temp[0].w,temp[1].w)+temp[2].w+temp[3].w;
        h = max(max(temp[2].h,temp[3].h),temp[0].h+temp[1].h);
        if(w*h<minarea){
            res.clear();
            minarea = w*h;
            if(!hash[min(w,h)][max(w,h)])
            res.push_back(make_pair(min(w,h),max(w,h))),hash[min(w,h)][max(w,h)]=1;
        }else if(w*h==minarea){
            if(!hash[min(w,h)][max(w,h)])
            res.push_back(make_pair(min(w,h),max(w,h))),hash[min(w,h)][max(w,h)]=1;
        }
case6:
        //这种情况注意处理,根据接触面来处理
        if(temp[0].h>temp[3].h){
            if(temp[0].h<temp[3].h+temp[2].h){
                w = max(max(temp[1].w+temp[2].w,temp[0].w+temp[3].w),temp[0].w+temp[2].w);
            }else{
                w = max(max(temp[0].w+temp[3].w,temp[0].w+temp[2].w),temp[1].w);
            }
        }else{
            if(temp[0].h+temp[1].h>temp[3].h){
                w = max(max(temp[1].w+temp[2].w,temp[3].w+temp[0].w),temp[3].w+temp[1].w);
            }else{
                w = max(max(temp[3].w+temp[0].w,temp[3].w+temp[1].w),temp[2].w);
            }
        }
        h = max(temp[0].h+temp[1].h,temp[2].h+temp[3].h);
        if(w*h<minarea){
            res.clear();
            minarea = w*h;
            if(!hash[min(w,h)][max(w,h)])
            res.push_back(make_pair(min(w,h),max(w,h))),hash[min(w,h)][max(w,h)]=1;
        }else if(w*h==minarea){
            if(!hash[min(w,h)][max(w,h)])
            res.push_back(make_pair(min(w,h),max(w,h))),hash[min(w,h)][max(w,h)]=1;
        }
        return;
    }
    for(int i=0;i<8;i++){
        int idx = i/2;
        if(!vis[idx<<1|1]&&!vis[idx<<1]){
            vis[i] = 1;
            temp[cur] = recs[i];
            dfs(cur+1);
            vis[i] = 0;
        }
    }
}
bool p_cmp(const pair<int,int> &a,const pair<int,int> &b){
    return a.first<b.first;
}
int main(){
    freopen("packrec.in","r",stdin);
    freopen("packrec.out","w",stdout);
    read();
    dfs(0);
    sort(res.begin(),res.end(),p_cmp);
    //while(1);
    printf("%d\n",res[0].first*res[0].second);
    for(int i=0;i<(int)res.size();i++){
        printf("%d %d\n",res[i].first,res[i].second);
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值