Skip to content

Instantly share code, notes, and snippets.

@Freaken
Created October 21, 2010 17:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Freaken/638941 to your computer and use it in GitHub Desktop.
Save Freaken/638941 to your computer and use it in GitHub Desktop.
/*
ID: IdolfHatler
PROG: packrec
LANG: C++
*/
#include <iostream>
#include <fstream>
#include <string>
#include <string.h>
#include <algorithm>
#define min3(a,b,c) min(min(a,b), c)
#define min4(a,b,c,d) min(min3(a, b, c), d)
#define max3(a,b,c) max(max(a,b), c)
#define max4(a,b,c,d) max(max3(a, b, c), d)
#define CUR layouts[perm][flip]
using namespace std;
struct rec {
int x;
int y;
const bool operator<(const rec &other) const {
return x < other.x || (x == other.x && y < other.y);
}
};
struct layout {
int size;
int x;
int y;
const bool operator<(const layout &other) const {
return size < other.size || (size == other.size && x < other.x);
}
};
int perm_two[24][4] = {
{0,1,2,3}, {0,1,3,2}, {0,2,1,3}, {0,2,3,1}, {0,3,1,2}, {0,3,2,1},
{1,0,2,3}, {1,0,3,2}, {1,2,0,3}, {1,2,3,0}, {1,3,0,2}, {1,3,2,0},
{2,0,1,3}, {2,0,3,1}, {2,1,0,3}, {2,1,3,0}, {2,3,0,1}, {2,3,1,0},
{3,0,1,2}, {3,0,2,1}, {3,1,0,2}, {3,1,2,0}, {3,2,0,1}, {3,2,1,0}
};
int main() {
ofstream fout ("packrec.out");
ifstream fin ("packrec.in");
rec rectangles[4];
layout layouts[24][16][5];
for(int n = 0; n < 4; n++) {
int x, y;
fin >> x >> y;
rectangles[n].x = x;
rectangles[n].y = y;
}
memset(layouts, 0, sizeof(layouts));
for(int perm = 0; perm < 24; perm++) {
for(int flip = 0; flip < 16; flip++) {
rec a, b, c, d;
memcpy(&a, &rectangles[perm_two[perm][0]], sizeof(a));
memcpy(&b, &rectangles[perm_two[perm][1]], sizeof(b));
memcpy(&c, &rectangles[perm_two[perm][2]], sizeof(c));
memcpy(&d, &rectangles[perm_two[perm][3]], sizeof(d));
int tmp;
if(flip & 1) {
tmp = a.x;
a.x = a.y;
a.y = tmp;
}
if(flip & 2) {
tmp = b.x;
b.x = b.y;
b.y = tmp;
}
if(flip & 4) {
tmp = c.x;
c.x = c.y;
c.y = tmp;
}
if(flip & 8) {
tmp = d.x;
d.x = d.y;
d.y = tmp;
}
// cout << ((flip&8) > 0);
// cout << ((flip&4) > 0);
// cout << ((flip&2) > 0);
// cout << ((flip&1) > 0);
// cout << perm_two[perm][0];
// cout << perm_two[perm][1];
// cout << perm_two[perm][2];
// cout << perm_two[perm][3];
// cout << " a(" << a.x << "," << a.y << ")";
// cout << " b(" << b.x << "," << b.y << ")";
// cout << " c(" << c.x << "," << c.y << ")";
// cout << " d(" << d.x << "," << d.y << ")";
// cout << endl;
// Layout 0
CUR[0].x = a.x + b.x + c.x + d.x;
CUR[0].y = max4(a.y, b.y, c.y, d.y);
// cout << "0: ";
// cout << CUR[0].x << " ";
// cout << CUR[0].y << " ";
// cout << endl;
// Layout 1
CUR[1].x = max(a.y, b.x+c.x+d.x);
CUR[1].y = max3(b.y, c.y, d.y)+a.x;
// cout << "1: ";
// cout << CUR[1].x << " ";
// cout << CUR[1].y << " ";
// cout << endl;
// Layout 2
CUR[2].x = max(a.y, b.x+c.x) + d.x;
CUR[2].y = max(a.x + max(b.y, c.y), d.y);
// cout << "2: ";
// cout << CUR[2].x << " ";
// cout << CUR[2].y << " ";
// cout << endl;
// Layout 3
CUR[3].x = a.x + max(b.x, c.x) + d.x;
CUR[3].y = max3(a.y, b.y+c.y, d.y);
// cout << "3: ";
// cout << CUR[3].x << " ";
// cout << CUR[3].y << " ";
// cout << endl;
// Layout 4
if(a.x > b.x || b.y > c.y) {
CUR[4].x = 999;
CUR[4].y = 999;
} else {
CUR[4].x = max(a.x+d.y, b.x+c.x);
CUR[4].y = max(a.y+b.y, c.y+d.x);
}
// cout << "4: ";
// cout << CUR[4].x << " ";
// cout << CUR[4].y << " ";
// cout << endl;
// cout << endl;
for(int n = 0; n < 5; n++) {
if(CUR[n].x > CUR[n].y) {
tmp = CUR[n].x;
CUR[n].x = CUR[n].y;
CUR[n].y = tmp;
}
CUR[n].size = CUR[n].x * CUR[n].y;
if(CUR[n].size <= 40) {
// cout << (flip&1 > 0) << (flip&2 > 0) << (flip&4 > 0) << (flip&8 > 0);
// cout << perm_two[perm][0];
// cout << perm_two[perm][1];
// cout << perm_two[perm][2];
// cout << perm_two[perm][3];
// cout << " " << n << " "<< CUR[n].x << " " << CUR[n].y << " " << CUR[n].size;
// cout << endl;
}
}
}
}
sort(&layouts[0][0][0], &layouts[24][0][0]);
int size = layouts[0][0][0].size;
int cur_x = -1;
for(int n = 0; n < 24*16*5; n++) {
// cout << layouts[0][0][n].size << " " << layouts[0][0][n].x << " " << layouts[0][0][n].y << endl;
}
fout << size << endl;
for(int n = 0; n < 24*16*5 && layouts[0][0][n].size == size; n++) {
if(layouts[0][0][n].x > cur_x) {
cur_x = layouts[0][0][n].x;
fout << layouts[0][0][n].x << " " << layouts[0][0][n].y << endl;
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment