Mondriaan's Dream
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 20653 | Accepted: 11646 |
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 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
状态枚举: 枚举每行所有可行状态
状态转移 :当前该行该状态种类数==符合当前行状态条件的上一行所有状态数的种类和。
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
#define MAX_ROW 12
#define MAX_STATUS 2048
long long dp[MAX_ROW][MAX_STATUS];
long long state[MAX_STATUS];
int a,b;
int tot;
bool TestFirstLine(int s) //第一行的判断要么是横着放,要么是竖着反。
{ //从右往左判断(每次取单个一)
//横放每次取两位,竖放每次取1个,当最前面的是1的话,最后两个序列一定是10不存在
int i=0;
while(i<b)
{
if(s&(1<<i)) //开头两个不能10 后面的不能有01
{
if(i==b-1||(s&(1<<(i+1)))==0) return false;
i+=2;
}
else i++;
}
return true;
}
bool CompatablityTest(int sa, int sb) // sa是当前状态,sb是上一状态
{
int i=0;
//对sa的每个位置的01进行判断
//从右向左判断
while(i<b)
{
if((sa&(1<<i))==0) //当当前的sa值为0时,不能竖着放,可以是不放的情况
{
if((sb&(1<<i))==0) return false;
i++;
}
else //当当前的sa值为1时
{
if((sb&(1<<i))==0) i++; //上一行的列位置为0 竖着放的情况
else if((i==b-1)||((sa&(1<<(i+1)))==0||(sb&(1<<(i+1)))==0)) return false; //考虑横着放的情况
else i+=2;
}
}
return true;
}
int main()
{
int i,j;
int k;
while(scanf("%d%d",&a,&b),a||b)
{
tot=0;
if(a%2==1&&b%2==1) {printf("0\n");continue;}
if(b>a) swap(b,a); //减少状态数,减少时间
int s=1<<b;
memset(dp,0,sizeof(dp));
for(j=0;j<s;j++) if(TestFirstLine(j)) dp[0][j]=1;
for(i=1;i<a;i++)
for(j=0;j<s;j++) //j是当前状态
for(k=0;k<s;k++) //k是当前状态
if(CompatablityTest(j,k)) dp[i][j]+=dp[i-1][k];
printf("%lld\n",dp[a-1][s- 1]); //最后一行所有的状态一定是全是1的情况
}
return 0;
}