USCAO section 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

有点恶心,要用全排列,可惜我还不会,数据小,就用几个for来搞定了,

需要注意的是最后一种情况,小心点就成了。

 
 
/*
ID:nealgav1
PROG:packrec
LANG:C++
*/
#include<cstdio>
#include<algorithm>
#include<stdlib.h>
#define N 123456
using namespace std;
struct A
{
  int w;
  int h;
};
struct B
{
  A sq[5];
  int are;
  int w;
  int h;
};
B area[N];
A cun[10];
A ans[N];
bool cmp(A a,A b)
{
  return a.h<b.h;
}
int main()
{
  freopen("packrec.in","r",stdin);
  freopen("packrec.out","w",stdout);
  while(scanf("%d%d",&cun[0].w,&cun[0].h)!=EOF)
  {
    for(int i=1;i<4;i++)
    scanf("%d%d",&cun[i].w,&cun[i].h);
    int kk=0;
    int mid;
    for(int i=0;i<4;i++)
    {
      for(int j=0;j<4;j++)
      {
        if(j==i)
        continue;
        for(int k=0;k<4;k++)
        {if(k==i||k==j)
          continue;
          for(int l=0;l<4;l++)
          {
            if(l==i||l==j||l==k)
            continue;
            for(int ii=0;ii<2;ii++)
            { if(ii)
              {
                mid=cun[0].h;
                cun[0].h=cun[0].w;
                cun[0].w=mid;
              }
              for(int jj=0;jj<2;jj++)
              {
                if(jj)
                {
                  mid=cun[1].h;
                cun[1].h=cun[1].w;
                cun[1].w=mid;
                }
                for(int kkk=0;kkk<2;kkk++)
                {
                  if(kkk)
                  {
                    mid=cun[2].h;
                    cun[2].h=cun[2].w;
                    cun[2].w=mid;

                  }
                  for(int ll=0;ll<2;ll++)
                  {
                    if(ll)
                    {
                      mid=cun[3].h;
                      cun[3].h=cun[3].w;
                      cun[3].w=mid;
                    }
                    area[kk].sq[0].w=cun[i].w;
            area[kk].sq[0].h=cun[i].h;
            area[kk].sq[1].w=cun[j].w;
            area[kk].sq[1].h=cun[j].h;
            area[kk].sq[2].w=cun[k].w;
            area[kk].sq[2].h=cun[k].h;
            area[kk].sq[3].w=cun[l].w;
            area[kk].sq[3].h=cun[l].h;
            kk++;
                  }
                }
              }
            }

          }
        }
      }
    }
    int g=0;int areare=9999999;
    for(int i=0;i<kk;i++)
    {
      //1
      int ll;
      int rr;
      ll=area[i].sq[0].h+area[i].sq[1].h+area[i].sq[2].h+area[i].sq[3].h;
      rr=max(area[i].sq[0].w,max(area[i].sq[1].w,max(area[i].sq[2].w,area[i].sq[3].w)));
      if(areare>ll*rr)
      {
        areare=ll*rr;
        g=0;
        if(ll<rr)
        {
          ans[g].h=ll;
          ans[g].w=rr;
        }
        else
        {
          ans[g].h=rr;
          ans[g].w=ll;
        }

      }
      else if(areare==ll*rr)
      {
        g++;
        if(ll<rr)
        {
          ans[g].h=ll;
          ans[g].w=rr;
        }
        else
        {
          ans[g].h=rr;
          ans[g].w=ll;
        }
      }
      //2
      ll=max(area[i].sq[0].h+area[i].sq[1].h+area[i].sq[2].h,area[i].sq[3].h);
      rr=area[i].sq[3].w+max(area[i].sq[0].w,max(area[i].sq[1].w,area[i].sq[2].w));
    if(areare>ll*rr)
      {
        areare=ll*rr;
        g=0;
        if(ll<rr)
        {
          ans[g].h=ll;
          ans[g].w=rr;
        }
        else
        {
          ans[g].h=rr;
          ans[g].w=ll;
        }
      }
      else if(areare==ll*rr)
      {
        g++;
        if(ll<rr)
        {
          ans[g].h=ll;
          ans[g].w=rr;
        }
        else
        {
          ans[g].h=rr;
          ans[g].w=ll;
        }
      }
      ll=max(area[i].sq[0].h+area[i].sq[1].h+area[i].sq[2].h,area[i].sq[2].h+area[i].sq[3].h);
      rr=max(area[i].sq[2].w,max(area[i].sq[0].w,area[i].sq[1].w)+area[i].sq[3].w);
       if(areare>ll*rr)
      {
        areare=ll*rr;
        g=0;
        if(ll<rr)
        {
          ans[g].h=ll;
          ans[g].w=rr;
        }
        else
        {
          ans[g].h=rr;
          ans[g].w=ll;
        }
      }
      else if(areare==ll*rr)
      {
        g++;
        if(ll<rr)
        {
          ans[g].h=ll;
          ans[g].w=rr;
        }
        else
        {
          ans[g].h=rr;
          ans[g].w=ll;
        }
      }
      ll=max(area[i].sq[0].h,max(area[i].sq[3].h,area[i].sq[1].h+area[i].sq[2].h));
      rr=max(area[i].sq[1].w,area[i].sq[2].w)+area[i].sq[0].w+area[i].sq[3].w;
       if(areare>ll*rr)
      {
        areare=ll*rr;
        g=0;
        if(ll<rr)
        {
          ans[g].h=ll;
          ans[g].w=rr;
        }
        else
        {
          ans[g].h=rr;
          ans[g].w=ll;
        }
      }
      else if(areare==ll*rr)
      {
        g++;
       if(ll<rr)
        {
          ans[g].h=ll;
          ans[g].w=rr;
        }
        else
        {
          ans[g].h=rr;
          ans[g].w=ll;
        }
      }
        rr=max(area[i].sq[0].w,area[i].sq[1].w)+max(area[i].sq[2].w,area[i].sq[3].w);
        ll=max(area[i].sq[0].h+area[i].sq[1].h,area[i].sq[2].h+area[i].sq[3].h);
        if(area[i].sq[0].w>=area[i].sq[1].w&&area[i].sq[1].h>=area[i].sq[3].h)
        rr=max(area[i].sq[0].w+area[i].sq[2].w,area[i].sq[1].w+area[i].sq[3].w);
      if(areare>ll*rr)
      {
        areare=ll*rr;
        g=0;
        if(ll<rr)
        {
          ans[g].h=ll;
          ans[g].w=rr;
        }
        else
        {
          ans[g].h=rr;
          ans[g].w=ll;
        }
      }
      else if(areare==ll*rr)
      {
        g++;
        if(ll<rr)
        {
          ans[g].h=ll;
          ans[g].w=rr;
        }
        else
        {
          ans[g].h=rr;
          ans[g].w=ll;
        }
      }
   }
   sort(ans,ans+g+1,cmp);
   printf("%d\n",areare);
   ans[g+1].h=-2;
   for(int j=0;j<=g;j++)
   { if(ans[j].h==ans[j+1].h)
     continue;
     printf("%d %d\n",ans[j].h,ans[j].w);
   }
  }
}

 
 
Packing Rectangles Russ Cox

This program is straightforward, but a bit long due to the geometry involved.

There are 24 permutations of the 4 rectangles, and for each permutation, 16 different ways to orient them. We generate all such orientations of permutations, and put the blocks together in each of the 6 different ways, recording the smallest rectangles we find.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

typedef struct Rect Rect;
struct Rect {
    int wid;
    int ht;
};

Rect
rotate(Rect r)
{
    Rect nr;

    nr.wid = r.ht;
    nr.ht = r.wid;
    return nr;
}

int
max(int a, int b)
{
    return a > b ? a : b;
}

int
min(int a, int b)
{
    return a < b ? a : b;
}

int tot;
int bestarea;
int bestht[101];

void
record(Rect r)
{
    int i;

    if(r.wid*r.ht < tot)
        *(long*)0=0;

    if(r.wid*r.ht < bestarea || bestarea == 0) {
        bestarea = r.wid*r.ht;
        for(i=0; i<=100; i++)
            bestht[i] = 0;
    }
    if(r.wid*r.ht == bestarea)
        bestht[min(r.wid, r.ht)] = 1;
}

void
check(Rect *r)
{
    Rect big;
    int i;

    /* schema 1: all lined up next to each other */
    big.wid = 0;
    big.ht = 0;
    for(i=0; i<4; i++) {
        big.wid += r[i].wid;
        big.ht = max(big.ht, r[i].ht);
    }
    record(big);

    /* schema 2: first three lined up, fourth on bottom */
    big.wid = 0;
    big.ht = 0;
    for(i=0; i<3; i++) {
        big.wid += r[i].wid;
        big.ht = max(big.ht, r[i].ht);
    }
    big.ht += r[3].ht;
    big.wid = max(big.wid, r[3].wid);
    record(big);

    /* schema 3: first two lined up, third under them, fourth to side */
    big.wid = r[0].wid + r[1].wid;
    big.ht = max(r[0].ht, r[1].ht);
    big.ht += r[2].ht;
    big.wid = max(big.wid, r[2].wid);
    big.wid += r[3].wid;
    big.ht = max(big.ht, r[3].ht);
    record(big);

    /* schema 4, 5: first two rectangles lined up, next two stacked */
    big.wid = r[0].wid + r[1].wid;
    big.ht = max(r[0].ht, r[1].ht);
    big.wid += max(r[2].wid, r[3].wid);
    big.ht = max(big.ht, r[2].ht+r[3].ht);
    record(big);

    /*
     * schema 6: first two pressed next to each other, next two on top, like: 
     * 2 3
     * 0 1
     */
    big.ht = max(r[0].ht+r[2].ht, r[1].ht+r[3].ht);
    big.wid = r[0].wid + r[1].wid;

    /* do 2 and 1 touch? */
    if(r[0].ht < r[1].ht)
        big.wid = max(big.wid, r[2].wid+r[1].wid);
    /* do 2 and 3 touch? */
    if(r[0].ht+r[2].ht > r[1].ht)
        big.wid = max(big.wid, r[2].wid+r[3].wid);
    /* do 0 and 3 touch? */
    if(r[1].ht < r[0].ht)
        big.wid = max(big.wid, r[0].wid+r[3].wid);

    /* maybe 2 or 3 sits by itself */
    big.wid = max(big.wid, r[2].wid);
    big.wid = max(big.wid, r[3].wid);
    record(big);    
}

void
checkrotate(Rect *r, int n)
{
    if(n == 4) {
        check(r);
        return;
    }

    checkrotate(r, n+1);
    r[n] = rotate(r[n]);
    checkrotate(r, n+1);
    r[n] = rotate(r[n]);
}

void
checkpermute(Rect *r, int n)
{
    Rect t;
    int i;

    if(n == 4)
        checkrotate(r, 0);

    for(i=n; i<4; i++) {
        t = r[n], r[n] = r[i], r[i] = t;    /* swap r[i], r[n] */
        checkpermute(r, n+1);
        t = r[n], r[n] = r[i], r[i] = t;    /* swap r[i], r[n] */
    }
}

void
main(void)
{
    FILE *fin, *fout;
    Rect r[4];
    int i;

    fin = fopen("packrec.in", "r");
    fout = fopen("packrec.out", "w");
    assert(fin != NULL && fout != NULL);

    for(i=0; i<4; i++)
        fscanf(fin, "%d %d", &r[i].wid, &r[i].ht);

    tot=(r[0].wid*r[0].ht+r[1].wid*r[1].ht+r[2].wid*r[2].ht+r[3].wid*r[3].ht);

    checkpermute(r, 0);
    fprintf(fout, "%d\n", bestarea);
    for(i=0; i<=100; i++)
        if(bestht[i])
            fprintf(fout, "%d %d\n", i, bestarea/i);
    exit(0);
}

 
USER: Neal Gavin Gavin [nealgav1]
TASK: packrec
LANG: C++

Compiling...
Compile: OK

Executing...
   Test 1: TEST OK [0.000 secs, 10584 KB]
   Test 2: TEST OK [0.000 secs, 10584 KB]
   Test 3: TEST OK [0.000 secs, 10584 KB]
   Test 4: TEST OK [0.000 secs, 10584 KB]
   Test 5: TEST OK [0.000 secs, 10584 KB]
   Test 6: TEST OK [0.000 secs, 10584 KB]
   Test 7: TEST OK [0.000 secs, 10584 KB]
   Test 8: TEST OK [0.000 secs, 10584 KB]
   Test 9: TEST OK [0.000 secs, 10584 KB]
   Test 10: TEST OK [0.000 secs, 10584 KB]
   Test 11: TEST OK [0.000 secs, 10584 KB]
   Test 12: TEST OK [0.000 secs, 10584 KB]
   Test 13: TEST OK [0.000 secs, 10584 KB]
   Test 14: TEST OK [0.000 secs, 10584 KB]
   Test 15: TEST OK [0.000 secs, 10584 KB]
   Test 16: TEST OK [0.000 secs, 10584 KB]
   Test 17: TEST OK [0.000 secs, 10584 KB]
   Test 18: TEST OK [0.000 secs, 10584 KB]
   Test 19: TEST OK [0.000 secs, 10584 KB]
   Test 20: TEST OK [0.000 secs, 10584 KB]
   Test 21: TEST OK [0.000 secs, 10584 KB]

All tests OK.

Your program ('packrec') produced all correct answers! This is your submission #2 for this problem. Congratulations!

Here are the test data inputs:

------- test 1 ----
1 2
2 3
3 4
4 5
------- test 2 ----
20 20
20 20
20 20
20 20
------- test 3 ----
4 5
5 4
4 5
16 1
------- test 4 ----
4 5
2 5
5 2
2 10
------- test 5 ----
12 18
4 6
2 17
19 3
------- test 6 ----
10 10
5 5
15 15
20 20
------- test 7 ----
1 1
1 20
1 20
20 20
------- test 8 ----
5 8
3 12
15 4
14 10
------- test 9 ----
4 5
5 6
6 4
4 5
------- test 10 ----
1 5
5 10
10 15
15 20
------- test 11 ----
3 4
8 5
7 1
4 5
------- test 12 ----
17 11
16 20
4 6
13 19
------- test 13 ----
4 2
2 6
2 3
5 8
------- test 14 ----
1 2
2 3
3 4
4 5
------- test 15 ----
4 8
8 12
12 16
16 20
------- test 16 ----
3 5
1 3
2 4
2 5
------- test 17 ----
4 3
4 4
6 3
5 5
------- test 18 ----
49 50
49 50
49 50
49 50
------- test 19 ----
10 50
45 30
28 38
36 20
------- test 20 ----
50 49
49 48
48 47
47 46
------- test 21 ----
50 49
48 47
46 45
45 44
Keep up the good work!


Thanks for your submission!
posted @ 2012-07-20 16:48  剑不飞  阅读(168)  评论(0编辑  收藏  举报