USACO Packing Rectangles

1、USACO真是相当重口,才1.4节就给了一道95年的IOI题目,如果不是事先知道用搜索做,而且还有测试数据(21组!)的话,以我的水平,真的很可能做不出来。

2、这个程序需要6个dfs,也就是每一种情况都要分别为它写一个dfs,最后一种“田”字型是最难的,要分成四类,刚开始因为思路不清晰在乱做,所以总是WA。一定要注意分类讨论要不重不漏,也就是大于等于的等于号不能少。

3、注意,标号为1,2,3,4的四个位置要轮流放一遍那些长方形,共4!种可能,而且每个长方形都有两种放法,所以这道题总共是6*4!*2^4种情况。

4、程序虽然很长,但是6个dfs只有微小差别。画个图可能更容易帮助理解。

5、哦,对了,因为没注意答案要按升序排列(其实注意到了,但是写着写着又忘了),所以WA了几次,一定要注意。

6、虽然一共交了十次才过,但我完全是独立思考并完成的,真的很有成就感!

/*
ID:mrxy564
PROG:packrec
LANG:C++
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct Rec{
    int l,w;
};
struct Sqr{
    int s,l,w;
    bool operator<(const Sqr &obj)const{
        return l<obj.l||l==obj.l&&w<obj.w;
    }
    bool operator ==(const Sqr &obj)const{
        return l==obj.l&&w==obj.w;
    }
    bool operator>(const Sqr &obj)const{
        return l>obj.l||l==obj.l&&w>obj.w;
    }
};
Rec a[4],b[4];
Sqr ans[5000];
int vis[4],cnt=0,minn;
void dfs1(int depth,Rec* b){
    if(depth==4){
        int len=0,wid=0;
        for(int i=0;i<4;i++){
          len=max(len,b[i].l);
          wid+=b[i].w;
        }
        ans[cnt].s=len*wid;
        ans[cnt].l=len;
        ans[cnt].w=wid;
        if(ans[cnt].s<minn) minn=ans[cnt].s;
        cnt++;
        return;
    }
    for(int i=0;i<4;i++){
      if(!vis[i]){
        vis[i]=1;
        b[depth].l=a[i].l;
        b[depth].w=a[i].w;
        dfs1(depth+1,b);
        b[depth].l=a[i].w;
        b[depth].w=a[i].l;
        dfs1(depth+1,b);
        vis[i]=0;
      }
    }
    return;
}
void dfs2(int depth,Rec* b){
    if(depth==4){
         int len=0,wid=0;
         for(int i=0;i<3;i++)
           len=max(len,b[i].l+b[3].l);
         wid=max(b[3].w,b[0].w+b[1].w+b[2].w);
         ans[cnt].s=len*wid;
         ans[cnt].l=len;
         ans[cnt].w=wid;
         if(ans[cnt].s<minn) minn=ans[cnt].s;
         cnt++;
         return;
    }
    for(int i=0;i<4;i++){
      if(!vis[i]){
        vis[i]=1;
        b[depth].l=a[i].l;
        b[depth].w=a[i].w;
        dfs2(depth+1,b);
        b[depth].l=a[i].w;
        b[depth].w=a[i].l;
        dfs2(depth+1,b);
        vis[i]=0;
      }
    }
    return;
}
void dfs3(int depth,Rec* b){
    if(depth==4){
         int len=0,wid=0;
         for(int i=0;i<2;i++)
           len=max(len,b[i].l+b[3].l);
         len=max(len,b[2].l);
         wid=max(b[0].w+b[1].w+b[2].w,b[3].w+b[2].w);
         ans[cnt].s=len*wid;
         ans[cnt].l=len;
         ans[cnt].w=wid;
         if(ans[cnt].s<minn) minn=ans[cnt].s;
         cnt++;
         return;
    }
    for(int i=0;i<4;i++){
      if(!vis[i]){
        vis[i]=1;
        b[depth].l=a[i].l;
        b[depth].w=a[i].w;
        dfs3(depth+1,b);
        b[depth].l=a[i].w;
        b[depth].w=a[i].l;
        dfs3(depth+1,b);
        vis[i]=0;
      }
    }
    return;
}
void dfs4(int depth,Rec* b){
    if(depth==4){
         int len=0,wid=0;
         len=max(b[0].l,b[3].l);
         len=max(len,b[1].l+b[2].l);
         wid=max(b[0].w+b[1].w+b[3].w,b[0].w+b[2].w+b[3].w);
         ans[cnt].s=len*wid;
         ans[cnt].l=len;
         ans[cnt].w=wid;
         if(ans[cnt].s<minn) minn=ans[cnt].s;
         cnt++;
         return;
    }
    for(int i=0;i<4;i++){
      if(!vis[i]){
        vis[i]=1;
        b[depth].l=a[i].l;
        b[depth].w=a[i].w;
        dfs4(depth+1,b);
        b[depth].l=a[i].w;
        b[depth].w=a[i].l;
        dfs4(depth+1,b);
        vis[i]=0;
      }
    }
    return;
}
void dfs5(int depth,Rec* b){
    if(depth==4){
         int len=0,wid=0;
         len=max(b[2].l,b[3].l);
         len=max(len,b[0].l+b[1].l);
         wid=max(b[0].w+b[2].w+b[3].w,b[1].w+b[2].w+b[3].w);
         ans[cnt].s=len*wid;
         ans[cnt].l=len;
         ans[cnt].w=wid;
         if(ans[cnt].s<minn) minn=ans[cnt].s;
         cnt++;
         return;
    }
    for(int i=0;i<4;i++){
      if(!vis[i]){
        vis[i]=1;
        b[depth].l=a[i].l;
        b[depth].w=a[i].w;
        dfs5(depth+1,b);
        b[depth].l=a[i].w;
        b[depth].w=a[i].l;
        dfs5(depth+1,b);
        vis[i]=0;
      }
    }
    return;
}
void dfs6(int depth,Rec* b){
    if(depth==4){
         int len,wid;
         if(b[1].l>=b[3].l&&b[1].w>=b[0].w||b[1].l<b[3].l&&b[1].w<b[0].w){
           len=wid=0;
           len=max(b[0].l+b[1].l,b[2].l+b[3].l);
           wid=max(b[0].w,b[1].w)+max(b[2].w,b[3].w);
           ans[cnt].s=len*wid;
           ans[cnt].l=len;
           ans[cnt].w=wid;
           if(ans[cnt].s<minn) minn=ans[cnt].s;
           cnt++;
         }
         if(b[1].l<b[3].l&&b[1].w>=b[0].w||b[1].l>=b[3].l&&b[1].w<b[0].w){
           len=wid=0;
           len=max(b[0].l+b[1].l,b[2].l+b[3].l);
           wid=max(b[0].w+b[2].w,b[1].w+b[3].w);
           ans[cnt].s=len*wid;
           ans[cnt].l=len;
           ans[cnt].w=wid;
           if(ans[cnt].s<minn) minn=ans[cnt].s;
           cnt++;
         }
         return;
    }
    for(int i=0;i<4;i++){
      if(!vis[i]){
        vis[i]=1;
        b[depth].l=a[i].l;
        b[depth].w=a[i].w;
        dfs6(depth+1,b);
        b[depth].l=a[i].w;
        b[depth].w=a[i].l;
        dfs6(depth+1,b);
        vis[i]=0;
      }
    }
    return;
}
int main(){
    freopen("packrec.in","r",stdin);
    freopen("packrec.out","w",stdout);
    for(int i=0;i<4;i++)
        scanf("%d%d",&a[i].l,&a[i].w);
    memset(b,0,sizeof(b));
    memset(ans,-1,sizeof(ans));
    memset(vis,0,sizeof(vis));
    minn=2147483647;
    dfs1(0,b);
    dfs2(0,b);
    dfs3(0,b);
    dfs4(0,b);
    dfs5(0,b);
    dfs6(0,b);
    for(int i=0;i<cnt;i++)
       if(ans[i].l>ans[i].w)
           swap(ans[i].l,ans[i].w);
    sort(ans,ans+cnt);
    cnt=unique(ans,ans+cnt)-ans;
    printf("%d\n",minn);
    for(int i=0;i<cnt;i++)
       if(ans[i].s==minn){
             printf("%d %d\n",ans[i].l,ans[i].w);
       }
    return 0;
}
官方题解:

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);
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值