HDU 4804 Campus Design(状压DP)

34 篇文章 1 订阅

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 10 9 + 7).
 

Sample Input
  
  
1 1 0 0 1 1 1 1 2 0 1 1 1 2 1 1 2 1 2 11 1 2 0 2 01 1 2 0 2 11 2 2 0 0 10 10 2 2 0 0 01 10 2 2 0 0 11 11 4 5 3 5 11111 11011 10101 11111
 

Sample Output
  
  
0 0 1 1 1 2 1 0 2 954
 

Source

题目大意:

    在一个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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值