Packing Rectangles

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

#include
#include
#include
#include
#include
#include
using namespace std;
typedef pair pill;
vector 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::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<     sort(q.begin(), q.end(), cmp);
    for(vector::iterator iter = q.begin(); iter != q.end(); iter++){
        fout<first<<" "<second<     }
    return 0;
}

你可能感兴趣的:(Packing Rectangles)