Packing Rectangles

//憋了两天终于熬出来了,用到了格雷码

#include<iostream>
#include<fstream>
#include<vector>
#include<utility>
#include<cmath>
#include<algorithm>
using namespace std;
typedef pair<int, int> pill;
vector<pill> q;
pill rect[4];
pill solution[20];
char  *Gray_code[20] = {"0000","0001","0011","0010","0110","0111","0101","0100","1100","1101","1111","1110","1010","1011","1001","1000"};
int strcmpp(char *a, char *b)
{
    for(int i = 0; i < 4;i++){
        if(a[i] != b[i]){
            return i;
        }
    }
}

void swap(int &x, int &y)
{
    int temp = y;
    y = x;
    x = temp;
}
void insert(int a, int b)
{
    if(a > b){
        swap(a, b);
    }
    if(!q.empty()){
        for(vector<pill>::iterator iter = q.begin(); iter != q.end(); iter++){
            if(a == iter->first){
                return ;
            }
        }
        q.push_back(make_pair(a,b));
    }else q.push_back(make_pair(a, b));
}

int cmp(const pill a, const pill b)
{
    return a.first < b.first;
}

int main()
{
    ofstream fout("packrec.out");
    ifstream fin("packrec.in");
    int i, j, k;
    int n, m;
    int a, b;
    int area = 100000000, longth = 0, width = 0, cnt = 0;
    for(i = 0; i < 4; i++){
        fin>>a>>b;
        if(a < b){
            rect[i].first = a;
            rect[i].second = b;
        }else{
            rect[i].first = b;
            rect[i].second = a;
        }
    }

    int r, s, l = 0, w = 0;
    for(r = 0; r < 16; r++){
        if(r != 0){
            s = strcmpp(Gray_code[r - 1], Gray_code[r]);
            swap(rect[s].first, rect[s].second);
        }
        longth = width = 0;
        for(i = 0; i < 4; i++){
            longth += rect[i].first;
            if(width < rect[i].second)
                width = rect[i].second;
        }
        if(area > longth * width){
            area = longth * width;
            q.clear();
            insert(longth, width);
        }else if(area == longth * width)
            insert(longth, width);
    }

    for(r = 0; r < 16; r++){
        if(r != 0){
            s = strcmpp(Gray_code[r - 1], Gray_code[r]);
            swap(rect[s].first, rect[s].second);
        }
        for(k = 0; k < 4; k++){
            longth = width = 0;
            for(i = 0; i < 4; i++){
                if(i == k)
                    continue;
                longth += rect[i].first;
                if(width < rect[i].second)
                    width = rect[i].second;
            }
            width += rect[k].first;
            if(longth < rect[k].second)
                longth = rect[k].second;
            if(area > longth * width){
                area = longth * width;
                q.clear();
                insert(longth, width);
            }else if(area == longth * width)
                insert(longth, width);
        }
    }


    for(r = 0; r < 16; r++){
        if(r != 0){
            s = strcmpp(Gray_code[r - 1], Gray_code[r]);
            swap(rect[s].first, rect[s].second);
        }
        for(i = 0; i < 4; i++){
            for(j = 0; j < 4; j++){
                if(i == j)
                    continue;
                longth = width = 0;
                for(k = 0; k < 4; k++){
                    if(k == i || k == j)
                        continue;
                    longth += rect[k].first;
                    if(width < rect[k].second)
                        width = rect[k].second;
                }
                if(longth < rect[j].second)
                    longth = rect[j].second + rect[i].first;
                else longth += rect[i].first;
                if(rect[i].second > width + rect[j].first)
                    width = rect[i].second;
                else width += rect[j].first;
                if(area > longth * width){
                    area = longth * width;
                    q.clear();
                    insert(longth, width);
                }else if(area == longth * width)
                    insert(longth, width);
            }
        }
    }
    for(r = 0; r < 16; r++){
        if(r != 0){
            s = strcmpp(Gray_code[r - 1], Gray_code[r]);
            swap(rect[s].first, rect[s].second);
        }
        for(i = 0; i < 4; i++){
            for(j = i + 1; j < 4; j++){
                width = rect[i].second;
                if(width < rect[j].second)
                    width = rect[j].second;
                w = 0;
                l = 0;
                for(k = 0; k < 4; k++){
                    if(k == i || k == j)
                        continue;
                    if(l < rect[k].first)
                        l = rect[k].first;
                    w += rect[k].second;
                }
                longth = rect[i].first + rect[j].first + l;
                if(width < w)
                    width = w;
                if(area > longth * width){
                    area = longth * width;
                    q.clear();
                    insert(longth, width);
                }else if(area == longth * width)
                    insert(longth, width);
            }
        }
    }
    int x;
    for(r = 0; r < 16; r++){
        if(r != 0){
            s = strcmpp(Gray_code[r - 1], Gray_code[r]);
            swap(rect[s].first, rect[s].second);
        }
        for(i = 0; i < 4; i++){
            longth = rect[i].first;
            for(j = 0; j < 4; j++){
                if(i == j)
                    continue;
                for(k = 0; k < 4; k++){
                    if(k == i || k == j)
                        continue;
                    for(x = 0; x < 4; x++){
                        if(x == i || x == j || x == k)
                            continue;
                        longth = rect[i].first + rect[j].first;
                        width = rect[i].second + rect[k].first;
                        if(rect[x].second <= rect[i].second - rect[j]. second){
                            if(longth < rect[i].first + rect[x].first)
                                longth = rect[i].first + rect[x].first;
                            if(longth < rect[k].second)
                                longth = rect[k].second;
                        }
                        else{
                            if(longth < rect[k].second + rect[x].first)
                                longth = rect[k].second + rect[x].first;
                            if(longth < rect[i].first + rect[x].first)
                                longth = rect[i].first + rect[x].first;
                            if(width < rect[x].second + rect[j].second)
                                width = rect[x].second + rect[j].second;
                            if(width < rect[k].first + rect[j].second)
                                width = rect[k].first + rect[j].second;
                        }
                        if(area > longth * width){
                            area = longth * width;
                            q.clear();
                            insert(longth, width);
                        }else if(area == longth * width)
                            insert(longth, width);
                    }
                }
            }
        }
    }
    fout<<area<<endl;
    sort(q.begin(), q.end(), cmp);
    for(vector<pill>::iterator iter = q.begin(); iter != q.end(); iter++){
        fout<<iter->first<<" "<<iter->second<<endl;
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值