POJ2411:Mondriaan's Dream(状压dp)

来源:互联网 发布:qt4 gui编程 编辑:程序博客网 时间:2024/06/05 04:45

Mondriaan's Dream
Time Limit: 3000MS Memory Limit: 65536KTotal Submissions: 16037 Accepted: 9283

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
题意:略。

思路:略。

待优化。

# include <stdio.h># include <string.h># define LL long longconst 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)        {            int c = a;            a = b;            b = c;        }        up = 1<<b;        init();        printf("%lld\n",fun());    }    return 0;}



0 0
原创粉丝点击