HDU 4804 Campus Design(状压DP)

来源:互联网 发布:淘宝商品被删除怎么办 编辑:程序博客网 时间:2024/06/16 12:29

Campus Design

Time Limit: 15000/8000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1235    Accepted Submission(s): 633


Problem Description
Nanjing University of Science and Technology is celebrating its 60th anniversary. In order to make room for student activities, to make the university a more pleasant place for learning, and to beautify the campus, the college administrator decided to start construction on an open space.
The designers measured the open space and come to a conclusion that the open space is a rectangle with a length of n meters and a width of m meters. Then they split the open space into n x m squares. To make it more beautiful, the designer decides to cover the open space with 1 x 1 bricks and 1 x 2 bricks, according to the following rules:

1. All the bricks can be placed horizontally or vertically
2. The vertexes of the bricks should be placed on integer lattice points
3. The number of 1 x 1 bricks shouldn’t be less than C or more than D. The number of 1 x 2 bricks is unlimited.
4. Some squares have a flowerbed on it, so it should not be covered by any brick. (We use 0 to represent a square with flowerbet and 1 to represent other squares)

Now the designers want to know how many ways are there to cover the open space, meeting the above requirements.
 

Input
There are several test cases, please process till EOF.
Each test case starts with a line containing four integers N(1 <= N <= 100), M(1 <= M <= 10), C, D(1 <= C <= D <= 20). Then following N lines, each being a string with the length of M. The string consists of ‘0’ and ‘1’ only, where ‘0’ means the square should not be covered by any brick, and ‘1’ otherwise.
 

Output
Please print one line per test case. Each line should contain an integers representing the answer to the problem (mod 109 + 7).
 

Sample Input
1 1 0 011 1 1 201 1 1 211 2 1 2111 2 0 2011 2 0 2112 2 0 010102 2 0 001102 2 0 011114 5 3 511111110111010111111
 

Sample Output
001112102954
 

Source
2013ACM/ICPC亚洲区南京站现场赛——题目重现

题目大意:

    在一个n*m的矩形中,有一些格子已经被占用,要用不少于C,不超过D个1*1的砖块和无限个1*2的填满,问方案数。


解题思路:

    由于这题的矩阵比较大,而且是长条形的,很容易想到轮廓线DP。由于砖块最多可以影响两行,所以要状压两行,不过这样时间应该会超出题目限制。那么我们就换一个状态表示方式,我们可以不像轮廓线dp一样一行一行的转移,如果我们一个一个的转移,我们就可以用dp[n][m][num][s],表示填完第n行第m列用了num的1*1还没有扫过的格子下一行位置的格子的状态(也就是我们可以状压一行的一部分和另一行的另一部分)。然后在对前两位用一下滚动数组优化,就可以把时间空间都降到可以接受的复杂度。


AC代码:

#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>using namespace std;#define mem(a,b) memset((a),(b),sizeof(a))const int MAXN=100+3;const int MAXM=10+3;const int MAXD=20+3;const int MOD=1000000000+7;int dp[2][MAXD][1<<MAXM];int N,M,C,D;char maze[MAXN][MAXM];//原地图int main(){    while(~scanf("%d%d%d%d",&N,&M,&C,&D))    {        mem(dp,0);        int MAXS=1<<M;        for(int i=0;i<N;++i)            scanf("%s",maze[i]);        int now=0,next=1;        dp[now][0][0]=1;        for(int i=0;i<N;++i)            for(int j=0;j<M;++j)            {                if(maze[i][j]=='1')//当前位置没有障碍                {                    for(int n=0;n<=D;++n)                    {                        for(int s=0;s<MAXS;++s)//枚举状态                            if(dp[now][n][s])                            {                                if(s&(1<<j))//当前位置已有,不放                                    dp[next][n][s^(1<<j)]=(dp[next][n][s^(1<<j)]+dp[now][n][s])%MOD;                                else                                {                                    if(n<=D)//使用1*1                                        dp[next][n+1][s]=(dp[next][n+1][s]+dp[now][n][s])%MOD;                                    if(j<M-1&&maze[i][j+1]=='1'&&!(s&(1<<(j+1))))//横放                                        dp[next][n][s|(1<<(j+1))]=(dp[next][n][s|(1<<(j+1))]+dp[now][n][s])%MOD;                                    if(i<N-1&&maze[i+1][j]=='1')//竖放                                        dp[next][n][s|(1<<j)]=(dp[next][n][s|(1<<j)]+dp[now][n][s])%MOD;                                }                            }                    }                }                else//有障碍                {                    for(int s=0;s<MAXS;++s)                        for(int n=0;n<=D;++n)                            if(dp[now][n][s])                                dp[next][n][s]=(dp[next][n][s]+dp[now][n][s])%MOD;                }                mem(dp[now],0);                swap(now,next);            }        int ans=0;        for(int i=C;i<=D;++i)            ans=(ans+dp[now][i][0])%MOD;        printf("%d\n",ans);    }        return 0;}


0 0
原创粉丝点击