希望的海洋

Ain't about how fast I get there,ain't about what's waiting on the other side

常用链接

统计

最新评论

usaco_packrec暴搜

这道题给了6个图,实际只需考虑5种(后两个相同)。最后一个模型自己没有抽象出来。还是逻辑清晰,比较不容易出错。头开始设许多临时变量,总是不知道具体值是多少,结果一直debug,还一直bug。。⊙﹏⊙b汗。。用4个rec结构记录比较好。
最后一个模型,是左边两个,右边两个,然后根据两边上面矩形的高度情况,讨论矩形整体的宽。
注意,自己设的变量最好还是写出来吧。此题竖着的边|,分别用b,w;横着的—用a,l;

/*
ID:greenwo1
PROG:packrec
LANG:C++
*/

#include
<cstdio>
#include
<cstring>
#include
<cmath>
#include
<algorithm>
#include
<cstdlib>

//#define MAX(a,b) (a)>(b)?(a):(b)
int cmp(const void* a,const void* b)
{
    
return *(int*)b-*(int*)a;
}

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

struct Rec{
    
int l,w;
}
;
int p[52],q[52],wid,hi,ct,area=1999999999,tmp;
bool used[5];
Rec tg[
4],bott,top,left,right,cur;
void lyot(int type,int a,int b,int ith)
{
    
int i;
    Rec saved,old;
    
if(type==3&&a==3&&b==2&&ith==2&&used[0]/*&&used[2]&&used[1]*/)////////////////
                {
                    area
=area;
                }

    
if(ith>=5)
    
{
        
if(cur.l*cur.w<area)
            
{
                area
=cur.l*cur.w;
                memset(p,
9,sizeof(p));
                memset(p,
9,sizeof(p));
                ct
=0;
                p[ct
++]=MAX(cur.l,cur.w);
                
if(area==24)////////////////
                {
                    area
=area;
                }

            }

            
else if(cur.l*cur.w==area)
            
{
                
for(i=0;i<ct;i++)
                
if(cur.l==p[i]||cur.w==p[i])break;
                
if(i==ct)p[ct++]=MAX(cur.l,cur.w);
                
/*cur.l=0;
                cur.w=0;
*/

                
            }

        
return;
    }

    old
=cur;
    
for(i=0;i<5;++i)//
        if(!used[i])
        
{    
            
if(i==4&&ith!=4)break;
            cur
=old;
            
switch(type)
            
{
            
/*    for(i=0;i<4;++i)
                        if(!used[i])break;
*/

            
case 0:    
                cur.w
=MAX(cur.w,b);
                cur.l
+=a;
                saved
=cur;
                
/*for(i=0;i<4;++i)
                    if(!used[i])break;
*/

                used[i]
=1;
                lyot(type,tg[i].l,tg[i].w,ith
+1);
                cur
=saved;
                lyot(type,tg[i].w,tg[i].l,ith
+1);

                used[i]
=0;
                
break;
            
case 1:
                
switch(ith)
                
{
                
case 1:
                    
//a=l,b=w
                    used[i]=1;
                    bott.l
=a;
                    bott.w
=b;
                    saved
=cur;
                    lyot(type,tg[i].l,tg[i].w,ith
+1);
                    cur
=saved;
                    lyot(type,tg[i].w,tg[i].l,ith
+1);//b swap a
                    used[i]=0;
                    wid
=0;
                    
break;
                
case 2:
                    used[i]
=1;
                    right.l
=a;
                    right.w
=b;
                    saved
=cur;
                    lyot(type,tg[i].l,tg[i].w,ith
+1);
                    cur
=saved;
                    lyot(type,tg[i].w,tg[i].l,ith
+1);//b swap a
                    used[i]=0;
                    
break;
                
case 3:
                    used[i]
=1;
                    left.l
=a;
                    left.w
=b;
                    saved
=cur;
                    lyot(type,tg[i].l,tg[i].w,ith
+1);
                    cur
=saved;
                    lyot(type,tg[i].w,tg[i].l,ith
+1);//b swap a
                    used[i]=0;
                    
break;
                
case 4:
                    cur.l
=MAX(a+left.l+right.l,bott.l);
                    cur.w
=MAX(MAX(left.w,right.w),b)+bott.w;
                    used[i]
=1;
                    saved
=cur;
                    lyot(type,tg[i].l,tg[i].w,ith
+1);
                    cur
=saved;
                    lyot(type,tg[i].w,tg[i].l,ith
+1);
                    used[i]
=0;
                    
break;
                
default:break;
                }

                    
break ;
            
case 2:
            
switch(ith)
                
{
                
case 1:
                    used[i]
=1;
                    bott.l
=a;
                    bott.w
=b;
                    saved
=cur;
                    lyot(type,tg[i].l,tg[i].w,ith
+1);
                    cur
=saved;
                    lyot(type,tg[i].w,tg[i].l,ith
+1);//b swap a
                    used[i]=0;
                    wid
=0;
                    hi
=0;
                    
break;
                
case 2:
                    used[i]
=1;
                    right.l
=a;
                    right.w
=b;
                    saved
=cur;
                    lyot(type,tg[i].l,tg[i].w,ith
+1);
                    cur
=saved;
                    lyot(type,tg[i].w,tg[i].l,ith
+1);
                    used[i]
=0;
                    
break;
                
case 3:
                    used[i]
=1;
                    left.l
=a;
                    left.w
=b;
                    saved
=cur;
                    lyot(type,tg[i].l,tg[i].w,ith
+1);
                    cur
=saved;
                    lyot(type,tg[i].w,tg[i].l,ith
+1);
                    used[i]
=0;
                    
break;
                
case 4:
                    cur.l
=MAX(left.l+a,bott.l)+right.l;
                    cur.w
=MAX(right.w,MAX(b,left.w)+bott.w);
                    used[i]
=1;
                    saved
=cur;
                    lyot(type,tg[i].l,tg[i].w,ith
+1);
                    cur
=saved;
                    lyot(type,tg[i].w,tg[i].l,ith
+1);
                    used[i]
=0;
                    
break;
                
default:break;
                }

                    
break ;
            
case 3:
                    
switch(ith)
                    
{
                    
case 1:
                        used[i]
=1;
    
                        right.w
=b;
                        right.l
=a;
                        saved
=cur;
                        lyot(type,tg[i].l,tg[i].w,ith
+1);
                        cur
=saved;
                        lyot(type,tg[i].w,tg[i].l,ith
+1);
                        used[i]
=0;
                        wid
=b;
                        hi
=0;
                        tmp
=0;
                        
break;
                    
case 2:
                        used[i]
=1;
                        top.w
=b;
                        top.l
=a;
                        saved
=cur;
                        lyot(type,tg[i].l,tg[i].w,ith
+1);
                        cur
=saved;
                        lyot(type,tg[i].w,tg[i].l,ith
+1);
                        used[i]
=0;
                        
break;
                    
case 3:
                        used[i]
=1;
                        bott.w
=b;
                        bott.l
=a;
                        saved
=cur;
                        lyot(type,tg[i].l,tg[i].w,ith
+1);
                        cur
=saved;
                        lyot(type,tg[i].w,tg[i].l,ith
+1);
                        used[i]
=0;
                        
break;
                    
case 4:
                        cur.l
=a+MAX(bott.l,top.l)+right.l;
                        cur.w
=MAX(right.w,MAX(bott.w+top.w,b));
                        used[i]
=1;
                        saved
=cur;
                        lyot(type,tg[i].l,tg[i].w,ith
+1);
                        cur
=saved;
                        lyot(type,tg[i].w,tg[i].l,ith
+1);
                        used[i]
=0;
                        
break;
                    
default:break;
                    }

                    
break ;
            
case 4:
                
switch(ith)
                
{
                
case 1:
                    top.w
=b;
                    top.l
=a;
                    used[i]
=1;
                    saved
=cur;
                    lyot(type,tg[i].l,tg[i].w,ith
+1);
                    cur
=saved;
                    lyot(type,tg[i].w,tg[i].l,ith
+1);
                    used[i]
=0;
                    
break;
                
case 2:
                    right.l
=a;
                    right.w
=b;
                    
/*cur.w=top.w+right.w;*/
                    used[i]
=1;
                    saved
=cur;
                    lyot(type,tg[i].l,tg[i].w,ith
+1);
                    cur
=saved;
                    lyot(type,tg[i].w,tg[i].l,ith
+1);
                    used[i]
=0;
                    
break;
                
case 3:
                    bott.w
=b;
                    bott.l
=a;
                    
/*cur.l=bott.l+right.l;
                    if(right.l<top.l)
                    cur.w=MAX(bott.w+top.w,cur.w);
                    else cur.w=MAX(bott.w,cur.w);
*/

                    used[i]
=1;
                    saved
=cur;
                    lyot(type,tg[i].l,tg[i].w,ith
+1);
                    cur
=saved;
                    lyot(type,tg[i].w,tg[i].l,ith
+1);
                    used[i]
=0;
                    
break;
                
case 4:
                    cur.w
=MAX(bott.w+b,top.w+right.w);
                    
if(right.w>=b+bott.w)cur.l=MAX(bott.l+right.l,MAX(a+right.l,top.l));
                    
else if(right.w<b+bott.w&&right.w>bott.w)cur.l=MAX(a+top.l,MAX(a+right.l,bott.l+right.l));
                    
else if(bott.w>=top.w+right.w)cur.l=MAX(a,MAX(top.l+bott.l,right.l+bott.l));
                    
else if(bott.w<top.w+right.w&&bott.w>=right.w)cur.l=MAX(bott.l+right.l,MAX(top.l+bott.l,a+top.l));
                    
/*cur.l=MAX(cur.l,MAX(top.l,right.l)+a);*/
                    used[i]
=1;
                    saved
=cur;
                    lyot(type,tg[i].l,tg[i].w,ith
+1);
                    cur
=saved;
                    lyot(type,tg[i].w,tg[i].l,ith
+1);
                    used[i]
=0;
                    
break;
                
default :break;
                }

            
break;
            
default :break;
            }

    }

}
    
int main()
{
    
int i,k,j;
    FILE 
*fin,*fout;
    fin
=fopen("packrec.in","r");
    fout
=fopen("packrec.out","w");
    
/*fin=freopen("packrec","r",stdin);
    fout=freopen("packrec","w",stdout);
*/

    
for(i=0;i<4;i++)
    
{
        fscanf(fin,
"%d%d",&k,&j);
        tg[i].l
=(k>j?k:j);
        tg[i].w
=(k<j?k:j);
    }

    
for(i=0;i<6;i++)
        
for(j=0;j<4;j++)
        
{
            used[j]
=1;
            cur.w
=0;cur.l=0;
            lyot(i,tg[j].w,tg[j].l,
1);
            cur.w
=0;cur.l=0;
            lyot(i,tg[j].l,tg[j].w,
1);
            used[j]
=0;
        }

    fprintf(fout,
"%d\n",area);
    qsort(p,ct,
sizeof(int),cmp);
    fprintf(fout,
"%d %d\n",area/p[0],p[0]);
    
for(i=1;i<ct;++i)
        
if(p[i]!=p[i-1])
            fprintf(fout,
"%d %d\n",area/p[i],p[i]);
    
return 0;
}


posted on 2011-08-24 18:21 拥梦的小鱼 阅读(385) 评论(0)  编辑 收藏 引用 所属分类: USACO


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   博问   Chat2DB   管理