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!
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
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
插头DP~
砖只有两种状态,横放和竖放,把横放记为两个0,竖放记为上1下0,逐格DP,每次无论前一格放什么,这一格都能放1,而如果前一格放了1,并且这一格是0,那么我们可以把前一格改成0,再把这一格也放上0组成一块。这样就做出了插头。
学神犇的写成了这种滚动数组的形式,空间减小了很多。
我是从1开始循环的,但这样容易出错,建议从0开始循环到n-1和m-1~
(这道题也能用状压DP做,但是慢很多,有兴趣可以看一下http://blog.csdn.net/senyelicone/article/details/56489597~)
#include<cstdio>
#include<cstring>
int n,m,c=1,p;
long long a[2][1<<11];
int main()
{
while(scanf("%d%d",&n,&m)==2 && n)
{
if(n*m%2)
{
printf("0\n");continue;
}
memset(a[c],0,sizeof(a[c]));a[c][0]=1;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
{
c^=1;p^=1;
memset(a[c],0,sizeof(a[c]));
for(int k=0;k<(1<<m);k++)
if(a[p][k])
{
a[c][k^(1<<(j-1))]+=a[p][k];
if(j!=1 && (k&(1<<(j-2))) && !(k&(1<<(j-1)))) a[c][k^(1<<(j-2))]+=a[p][k];
}
}
printf("%lld\n",a[c][0]);
}
return 0;
}