【CEOI2002】poj1038 Bugs Integrated, Inc.

来源:互联网 发布:魔法王座神器升级数据 编辑:程序博客网 时间:2024/06/02 08:25

Description Bugs Integrated, Inc. is a major manufacturer of advanced
memory chips. They are launching production of a new six terabyte
Q-RAM chip. Each chip consists of six unit squares arranged in a form
of a 2*3 rectangle. The way Q-RAM chips are made is such that one
takes a rectangular plate of silicon divided into N*M unit squares.
Then all squares are tested carefully and the bad ones are marked with
a black marker.

Finally, the plate of silicon is cut into memory chips. Each chip
consists of 2*3 (or 3*2) unit squares. Of course, no chip can contain
any bad (marked) squares. It might not be possible to cut the plate so
that every good unit square is a part of some memory chip. The
corporation wants to waste as little good squares as possible.
Therefore they would like to know how to cut the plate to make the
maximum number of chips possible. Task You are given the dimensions of
several silicon plates and a list of all bad unit squares for each
plate. Your task is to write a program that computes for each plate
the maximum number of chips that can be cut out of the plate.

Input The first line of the input file consists of a single integer D
(1 <= D <= 5), denoting the number of silicon plates. D blocks follow,
each describing one silicon plate. The first line of each block
contains three integers N (1 <= N <= 150), M (1 <= M <= 10), K (0 <= K
<= MN) separated by single spaces. N is the length of the plate, M is
its height and K is the number of bad squares in the plate. The
following K lines contain a list of bad squares. Each line consists of
two integers x and y (1 <= x <= N, 1 <= y <= M) ?coordinates of one
bad square (the upper left square has coordinates [1, 1], the bottom
right is [N,M]).

Output For each plate in the input file output a single line
containing the maximum number of memory chips that can be cut out of
the plate.

状压dp。
考虑到每一层的状态与上面两层有关,因此每层转移到下一层的有用信息只有两层,需要用三进制保存状态。
但是这样就不太好写位运算了,需要单独写子程序完成数和状态数组的转换。
0表示这一位和上一位都为空,1表示这一位空,上一位不空,2表示这一位不空。
那么用每一层向下转移。
具体做法是先处理出下一层什么都不放的状态【如果有坏点记为2】,然后用这个状态进行dfs枚举各种放的情况并更新答案。
具体枚举方法见代码。
因为占用空间比较大,采用滚动数组。

#include<cstdio>#include<cstring>int max(int a,int b){    return a>b?a:b;}int pow[11]={1,3,9,27,81,243,729,2187,6561,19683,59049},dp[2][60000],m,n,map[160][15];struct cond{    int a[12];}last;cond arr(int x){    int i,j,k;    cond c1;    for (i=m;i>=2;i--)    {        c1.a[i]=x/pow[i-1];        x-=c1.a[i]*pow[i-1];    }    c1.a[1]=x;    return c1;}int num(cond c1){    int i,j,k,x=0;    for (i=1;i<=m;i++)      x+=c1.a[i]*pow[i-1];    return x;}void dfs(int k,cond c1,int p,int now){    int i,j,x,y,z;    x=num(c1);    dp[k&1][x]=max(dp[k&1][x],now);    if (p>=m) return;    if (p<m-1&&c1.a[p]==0&&c1.a[p+1]==0&&c1.a[p+2]==0)    {        c1.a[p]=c1.a[p+1]=c1.a[p+2]=2;        dfs(k,c1,p+3,now+1);        c1.a[p]=c1.a[p+1]=c1.a[p+2]=0;    }    if (c1.a[p]==0&&c1.a[p+1]==0&&last.a[p]==0&&last.a[p+1]==0)    {        c1.a[p]=c1.a[p+1]=last.a[p]=last.a[p+1]=2;        dfs(k,c1,p+2,now+1);        c1.a[p]=c1.a[p+1]=last.a[p]=last.a[p+1]=0;    }    dfs(k,c1,p+1,now);}int main(){    int i,j,k,x,y,z,T,cnt,ans;    cond c1,c2;    scanf("%d",&T);    while (T--)    {        memset(dp,0xff,sizeof(dp));        memset(map,0,sizeof(map));        scanf("%d%d%d",&n,&m,&cnt);        while (cnt--)        {            scanf("%d%d",&x,&y);            map[x][y]=1;        }        dp[0][pow[m]-1]=0;        for (i=0;i<n;i++)        {            for (k=0;k<pow[m];k++)              if (dp[i&1][k]>=0)              {                  c1=arr(k);                  last=c1;                  for (j=1;j<=m;j++)                    if (map[i+1][j])                      c2.a[j]=2;                    else                      c2.a[j]=max(c1.a[j]-1,0);                  dfs(i+1,c2,1,dp[i&1][k]);              }            for (k=0;k<pow[m];k++)              dp[i&1][k]=-1;        }        ans=0;        for (i=0;i<pow[m];i++)          ans=max(ans,dp[n&1][i]);        printf("%d\n",ans);    }}
0 0
原创粉丝点击