POJ 2411 Mondriaan's Dream (状压DP)

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



题意:给定一个n*m的方格矩形,求用1 * 2和2 * 1的小矩形完全覆盖的方案数,n <= 11,m <= 11.

解题思路:假设第一列已经填满,则第二列的摆设方式,只与第一列对第二列的影响有关。同理,第三列的摆设方式也只与第二列对它的影响有关。那么,使用一个长度为N的二进制数j来表示这个影响,
dp[i][j]表示到第i行状态为j的方案数,i<=M,0<=j<2N
那么dp[i][j] += dp[i][k] (if (check(j->k)));
当上一行的状态为1,当前行为0,或者上一行为0,当前行为1,肯定可以,但当上一行和当前行都为1式,需要判断上一行和下一行是不是两个连续的1,(即同时摆放2*1的小块);
详细思路见代码:

#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
long long dp[20][1<<11];
//dp[i][j]表示到第i行状态为j的方案数,那么dp[i][j] += dp[i][k] (if (check(j->k)))
int n,m;
int check(int pre,int cur)//上一行状态pre,对当前行的影响cur
{
    for (int i=0;i<m;i++){
        int t1=pre&(1<<i);//枚举上一行的每位
        int t2=cur&(1<<i);//枚举当前行的每位
        if(!t1&&!t2)//都为0,表示都未被覆盖,返回0;
            return 0;
        if(t1&&t2){//都为1,需要判断下一行的状态
            i++;
            if (i==m)//到边界,不合法,返回0;
                return 0;
            //需要上一行和当前行都是两个连续的1;
            if ((pre&(1<<i))==0)
                return 0;
            if ((cur&(1<<i))==0)
                return 0;
        }
    }
    //一个0,一个1,返回1;
    return 1;
}
int main()
{
    while(scanf("%d%d",&n,&m),n+m){
        if (m>n)
            swap(n,m);//也是剪枝
        if (n%2&&m%2){//n和m同为奇数输出为0,进行剪枝
            printf("0\n");
            continue;
        }
        memset(dp,0,sizeof(dp));
        dp[0][(1<<m)-1]=1;//从0行开始枚举
        for(int i=0;i<n;i++)
           for(int j=0;j<(1<<m);j++)//用二进制0,1表示是否被覆盖,枚举上一行的状态
                if(dp[i][j])//剪枝
                    for(int k=0;k<(1<<m);k++)//枚举当前行的状态
                        if(check(j,k))//判断当前行是否合法
                            dp[i+1][k]+=dp[i][j];
        printf("%lld\n",dp[n][(1<<m)-1]);
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值