A - Mondriaan's Dream

Squares and rectangles fascinated the famous Dutch painter Piet Mondriaan. One night, after producing the drawings in his 'toilet series' (where he had to use his toilet paper to draw on, for all of his paper was filled with squares and rectangles), he dreamt of filling a large rectangle with small rectangles of width 2 and height 1 in varying ways. 

Expert as he was in this material, he saw at a glance that he'll need a computer to calculate the number of ways to fill the large rectangle whose dimensions were integer values, as well. Help him, so that his dream won't turn into a nightmare!
Input
The input contains several test cases. Each test case is made up of two integer numbers: the height h and the width w of the large rectangle. Input is terminated by h=w=0. Otherwise, 1<=h,w<=11.
Output
For each test case, output the number of different ways the given rectangle can be filled with small rectangles of size 2 times 1. Assume the given large rectangle is oriented, i.e. count symmetrical tilings multiple times.
Sample Input
1 2
1 3
1 4
2 2
2 3
2 4
2 11
4 11
0 0
Sample Output
1
0
1
2
3
5
144
51205

      给一个N*M的矩阵,要求用1*2的小矩阵填满,求共多少种方法。

      首先定义(i,j)点铺砖方式:

          1、横着铺  (i,j) (i,j+1)都记为1

          2、竖着铺 (i,j)记为0,(i+1,j)记为1

      然后对于(i,j)铺砖时要考虑情况:

          1、(i-1,j)为0,那么说明(i,j)已经被上行铺完了,只能为1。

          2、(i-1,j)为1,(i,j)可以选择横着(1)、竖着(0)

      然后对于每一行,M个0/1,共1<<M中状态,进行压缩,dp【i】【j】表示铺第i行是状态为j的方法数。

      进行dp过程中,要考虑首行:不是每一种状态都可以放在首行,从前往后判断过程中,要求第j列为1时第j+1列一定为1(即必须两个连续1)。

代码如下:

#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long LL;
int N, M;
LL dp[12][1<<12];
int init(int x)//判断x
{
    for(int i=0; i<M;) //对第i列
    {
        if(x & (1<<i))//第i列为1
        {
            if(i==M-1) //i为最后一列,不可行
                return 0;
            if(x & (1<<(i+1))) //第i列和第i+1列都为1,横放,考虑第i+2列
                i+=2;
            else
                return 0;
        }
        else //第i列为0 ,竖放,可行
            i++;
    }
    return 1;
}
int check(int now, int pre)
{
    for(int i=0; i<M; )
    {
        if(now & (1<<i)) //now行第i列为1  pre可以为0/1
        {
            if (pre & (1<<i)) //pre行第i列为1,那么第i行 横放
            {
                //第i行和第i-1行的第j+1都必须是1
                if (i==M-1 || !(now & 1<<(i+1)) || !(pre & 1<<(i+1)))
                    return 0;
                else
                    i+=2;
            }
            else //pre行第i列为0,竖放
                i++;
        }
        else  //now行第i列为0  pre只能为1
        {
            if(pre & (1<<i))//pre为1,竖放
                i++;
            else
                return 0;
        }
    }
    return 1;
}
int main()
{
    //ios::sync_with_stdio(0);
    while(scanf("%d%d", &N, &M) && N && M)
    {
        if((N & 1) && (M & 1))//N、M都为奇数,面积为奇数
        {
            printf("0\n");
            continue;
        }
        if(N<M)
        {
            int temp=N;
            N=M;
            M=temp;
        }
        int MM=(1<<M);
        memset(dp, 0, sizeof(dp));
        for(int i=0; i<MM; i++)//初始化首行
        {
            if(init(i))
                dp[1][i]=1;
        }
        for(int i=2; i<=N; i++)//i= 2-N 行
        {
            for(int j=0; j<MM; j++) //第i行的状态
            {
                for(int k=0; k<MM; k++) //第i-1行的状态
                {
                    if(check(j, k))
                        dp[i][j]+=dp[i-1][k];
                }
            }
        }
        printf("%lld\n", dp[N][MM-1]);
        //cout<<dp[N][MM-1]<<endl;
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值