【★】tiling

POJ 2411
Mondriaan’s Dream Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 3322 Accepted: 1914

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

Ulm Local 2000




// author: jfo()

////////////////////////////////////////////////////////////////////////////

#include <iostream>
#include <algorithm>
using namespace std;

typedef long long SUM;
int h, w;
SUM record[12][2048];
int possib[2048];

int possible(unsigned int fill)
{
int flag = 0;
while(fill) {
if(fill & 0x1)
flag = flag ^ 0x1;
else {
if(flag)
return 0;
}
fill = fill >> 1;
}
return !flag;
}

int count_1(unsigned int n)
{
int c = 0;
while(n) {
if(n & 1)
c++;
n = n >> 1;
}
return c;
}

/ 第height层(以0层开始)放置有j(竖直放置,占用height层和height-1层) /
SUM fn(int height, int j)
{
if((w * height + count_1(j)) % 2)
return 0;

if(record[height][j] >= 0)
return record[height][j];

if(0 == height)
return j ? 0 : 1;

SUM sum = 0;
for(int i = 0; i < (2 << w); i++) {
if(i & j)
continue;
if(!possible(((1 << w) - 1) & (~(i | j))))
continue;
sum += fn(height - 1, i);
}
record[height][j] = sum;
return sum;
}

int main()
{
while(cin >> h >> w && h > 0) {
if(w < h)
swap(w, h);
for(int i = 0; i <= h; i++)
memset(record[i], -1, sizeof(record[i]));
//for(int i = 0; i <= h; i++)
// for(int j = 0; j < 2048; j++)
// record[i][j] = -1;
memset(possib, -1, sizeof(possib));
cout << fn(h, 0) << endl;
}

return 0;
}