sgu131 Hardwood floor

131. Hardwood floor

time limit per test: 0.25 sec. 
memory limit per test: 4096 KB

The banquet hall of Computer Scientists' Palace has a rectangular form of the size M x N (1<=M<=9, 1<=N<=9). It is necessary to lay hardwood floors in the hall. There are wood pieces of two forms:
1) rectangles (2x1) 
2) corners (squares 2x2 without one 1x1 square) 
You have to determine X - the number of ways to cover the banquet hall. 
Remarks. The number of pieces is large enough. It is not allowed to leave empty places, or to cover any part of a surface twice, or to saw pieces.

Input

The first line contains natural number M. The second line contains a natural number N.

Output

First line should contain the number X, or 0 if there are no solutions.

Sample Input

2 3

Sample Output

5


轮廓线DP。由于这两种放法会占用下一填充块的下方位置。所以边界需将当前填充块的下方考虑进去。   压缩状态时加一位,表示该位置。

代码:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
long long  dp[2][1500];
int main()
{
    int m,n,cnt,i,j,k;
    scanf("%d%d",&m,&n);
    memset(dp,0,sizeof(dp));
    cnt=0;
    dp[0][0]=1;
    for(i=0;i<m;i++)
    {
        for(j=0;j<n;j++)
        {
            memset(dp[cnt^1],0,sizeof(dp[cnt^1]));
            for(k=0;k<1<<(n+1);k++)
            {
                if(dp[cnt][k]==0)   continue;
                if(k>>j&1)
                {
                    if(k>>n&1)
                        dp[cnt^1][k&~(1<<n)]+=dp[cnt][k];
                    else
                        dp[cnt^1][k&~(1<<j)]+=dp[cnt][k];
                }
                else
                {
                    //1*2砖块
                    if(j<n-1&&!(k>>(j+1)&1))
                    {
                        if(k>>n&1)
                           dp[cnt^1][k&(~(1<<n))|(1<<(j+1))|1<<j]+=dp[cnt][k];

                        else
                            dp[cnt^1][k|(1<<(j+1))]+=dp[cnt][k];
                    }
                    if(i<m-1&&!(k>>n&1))
                        dp[cnt^1][k|(1<<j)]+=dp[cnt][k];

                    //3砖块
                    if(j<n-1&&i<m-1&&!(k>>(j+1)&1)&&!(k>>n&1))
                        dp[cnt^1][k|(1<<j)|(1<<(j+1))]+=dp[cnt][k];

                    if(j<n-1&&i<m-1&&!(k>>n&1))
                        dp[cnt^1][k|(1<<j)|(1<<n)]+=dp[cnt][k];

                    if(j>0&&i<m-1&&!(k>>(j-1)&1)&&!(k>>n&1))
                        dp[cnt^1][k|(1<<(j-1))|(1<<j)]+=dp[cnt][k];

                    if(j<n-1&&i<m-1&&!(k>>(j+1)&1))
                    {
                        if(k>>n&1)
                            dp[cnt^1][k|(1<<j)|1<<(j+1)]+=dp[cnt][k];
                        else
                            dp[cnt^1][k|(1<<(j+1)|(1<<n))]+=dp[cnt][k];
                    }
                }
            }
            cnt^=1;
        }
    }
    printf("%lld\n",dp[cnt][0]);
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值