poj2411 mondriaan's dream 状压dp

来源:互联网 发布:网络维护班组优秀案例 编辑:程序博客网 时间:2024/06/16 09:05

Language:
Mondriaan's Dream
Time Limit: 3000MS Memory Limit: 65536KTotal 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 21 31 42 22 32 42 114 110 0

Sample Output

10123514451205

Source

Ulm Local 2000




题意:一个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:稍后补



0 0
原创粉丝点击