poj 2411 Mondriaan's Dream 【状压dp】【经典状压】

本文介绍了一个算法问题,即如何计算用大小为2×1的小矩形填充一个给定的大矩形的方法数量。通过分析不同填充方式的有效性和合法性,提供了一段实现该算法的C++代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Description

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

分析:

如果一个骨牌是横着放的,那么它所在的两个方格都填充1.如果它是竖着放的,那么它所在的两个格子中,上面的那个填0,下面的这个填1。

1.第一行的情况,如果1不是连续的偶数对就非法。

2.相邻的两行,如果相邻的两行按位与之后的状态不合法,那么就不合法。【关键】

3.保证相邻两行合法的条件是按位或为(1<<m)-1。

代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
#define ll long long

const int MAXN = (1<<11)+1;
ll dp[MAXN], tmp[MAXN];
bool ok[MAXN];
int a, b, up;

bool judge(int i)
{
    while(i)
    {
        if(i&1)
        {
            i >>= 1;
            if(!(i&1))
                return false;
            i >>= 1;
        }
        else
            i >>= 1;
    }
    return true;
}

void init()
{
    memset(ok, false, sizeof(ok));

    memset(tmp, 0, sizeof(tmp));
    for(int i=0; i<up; ++i)
        if(judge(i))
        {
            ok[i] = true;
            tmp[i] = 1;//首行必须要相邻的1,或者没1。
        }
}

ll fun()
{
    for(int i=2; i<=a; ++i)
    {
        memset(dp, 0, sizeof(dp));
        for(int j=0; j<up; ++j)
            for(int k=0; k<up; ++k)
            {
                if(!tmp[k] || (j|k)!=up-1)
                    continue;
                if(!ok[j&k])//上下两种状态按位与后必须要有两个相邻的1,或者没有1。
                    continue;
                dp[j] += tmp[k];
            }
        for(int i=0; i<up; ++i)
            tmp[i] = dp[i];
    }
    return dp[up-1];
}

int main()
{
    while(~scanf("%d%d",&a,&b),a+b)
    {
        if((a*b)&1)
        {
            puts("0");
            continue;
        }
        if(a < b)
        {
            swap(a,b);
        }
        up = 1<<b;
        init();
        printf("%lld\n",fun());
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值