POJ - 2411 Mondriaan's Dream(状态压缩DP)

来源:互联网 发布:短域名生成 编辑:程序博客网 时间:2024/05/23 16:01

思路:状压DP


#include<iostream>#include<cstdio>#include<cstring>using namespace std;const int maxn = 12;#define LL long longLL dp[2][(1<<15)];int path[(1<<12)*(1<<12)][2];int w;void get(int m)   {    for(int i = 0;i<(1<<m);i++){for(int j = 0;j<(1<<m);j++){bool ok = true;           for(int k = 0;k<m;k++)    {                if((i&(1<<k))==0)       {                    if((j&(1<<k))==0){ok = false;break;}}else                {                    if((j&(1<<k))==0)    continue;k++;if((i&(1<<(k-1)) && !(i&(1<<k))) || k>=m)   {ok = false;break;}                    else                         {                         if((j&(1<<k))==0 && j&(1<<(k-1))) { ok = false; break; } /*if(j&(1<<k) && !(j&(1<<(k-1)))) { k--; }*/}}}if(ok){path[w][0]=i;path[w++][1]=j;}}}}int main(){    int n,m;while(scanf("%d%d",&n,&m)!=EOF && (n+m)){w=0;if(n<m)swap(n,m);get(m);memset(dp,0,sizeof(dp));dp[0][(1<<m)-1]=1;int cur = 0;for(int i = 0;i<n;i++){cur^=1;memset(dp[cur],0,sizeof(dp[cur]));for(int j = 0;j<w;j++){dp[cur][path[j][1]]+=dp[1-cur][path[j][0]];}}printf("%lld\n",dp[cur][(1<<m)-1]);}}


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


0 0
原创粉丝点击