poj2411 mondriaan's dream 状压dp

探讨如何计算使用1*2的矩形块填充任意m*n矩形的所有不同方式,通过巧妙的状态压缩和动态规划方法实现。

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

Language:
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 10925 Accepted: 6356

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

Source





题意:一个m*n的矩阵,只能放1*2的木块,问将这个矩阵完全覆盖的不同放法有多少种。

思路1:横着的1*2矩阵设为1 1,竖着的1*2矩阵设为0 1。首先预处理第一行,排除奇数个连续1的情况,存储到s数组;接下来,若第i行的状态为s1,第i-1行的状态为s2,若s2合法,则s2|s1必然等于(1<<n)-1,因为不存在竖着的矩阵为 0 0;之后再判断下第i行横着放的木块是是否有奇数个连续1的情况发生,即判s[s1& s2],其中s1&s2很好的屏蔽了竖着的1。详见代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int MAXN=15;
int s[MAXN];
ll dp[MAXN][1<<MAXN];
int m,n;
inline bool check(int x) // 检查是否会出现奇数个连续1
{
    int bit=0;
    while(x)
    {
        if(x&1)
            bit++;
        else if(bit &1)
            return 0;
        else
            bit=0;
        x>>=1;
    }
    if(bit&1)
        return 0;
    return 1;
}
inline bool ok(int x,int y)
{
    if((x|y)!=(1<<n)-1)
        return 0;
    return s[x&y];//这个很妙,将竖型的1屏蔽,判断是否会有奇数个1
}
void init()
{
    memset(s,0,sizeof(s));
    memset(dp,0,sizeof(dp));
    for(int i=0;i<(1<<n);i++)
        if(check(i))
            s[i]=1;
}
int main()
{
    //freopen("text.txt","r",stdin);
    while(~scanf("%d%d",&n,&m) && n+m)
    {
        if((n*m)&1)
        {
            printf("0\n");
            continue;
        }
        if(m<n)
            swap(m,n);
        init();
        for(int i=0;i<(1<<n);i++)
            if(s[i])
                dp[1][i]=1;
        for(int i=2;i<=m;i++)
        {
            for(int j=0;j<(1<<n);j++)
                for(int k=0;k<(1<<n);k++)
                    if(ok(j,k))
                        dp[i][j]+=dp[i-1][k];
        }
        printf("%lld\n",dp[m][(1<<n)-1]);// 选(1<<n)-1的状态是因为无论倒数第二行是竖是横,最后一行一定全是1
    }
    return 0;
}

思路2:稍后补



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值