POJ 2411 Mondriaan's Dream (状压DP)

来源:互联网 发布:淘宝联盟推广 编辑:程序博客网 时间:2024/05/16 12:38
Mondriaan's Dream
Time Limit: 3000MS Memory Limit: 65536KTotal Submissions: 17232 Accepted: 9938

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


dp[i][j]表示前i-1行已摆好,第i行的状态为j时的方案数

三种摆放方式:

(1)横放,状态为1 1

(2)竖放,占本行和上一行,本行状态为1,上一行状态为0

(3)竖放,占本行和下一行,本行状态为0,下一行状态为1


#include <stdio.h>#include <string.h>#include <math.h>#include <algorithm>#define maxn 1<<13int const inf=1000000000;using namespace std;int n,m;long long dp[15][1<<12];void init(int s,int p){if(p==m){dp[0][s]=1;return ;}if(p+1<=m)init(s<<1,p+1);if(p+2<=m)init(s<<2|3,p+2);}void dfs(int i,int cur,int per,int p){if(p==m){dp[i][cur]+=dp[i-1][per];return ;}if(p+1<=m){dfs(i,cur<<1|1,per<<1,p+1);dfs(i,cur<<1,per<<1|1,p+1);}if(p+2<=m)dfs(i,cur<<2|3,per<<2|3,p+2);}int main (){int i;while(scanf("%d %d",&n,&m)!=EOF){memset(dp,0,sizeof(dp));if(n==0&&m==0)break;if((n*m)%2){printf("0\n");continue;}init(0,0);for(i=1;i<n;i++)dfs(i,0,0,0);printf("%lld\n",dp[n-1][(1<<m)-1]);}return 0;}


原创粉丝点击