Mondriaan's Dream
Time Limit: 3000MS | Memory Limit: 65536K | |
Total 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!
![](https://i-blog.csdnimg.cn/blog_migrate/91cd2a849209448a71d50fa577e5d5ed.jpeg)
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
![](https://i-blog.csdnimg.cn/blog_migrate/9275391b5c2fd4e3cb556e4ab10140b5.jpeg)
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
Source
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<<13
int 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;
}