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
状态压缩动态规划
1: #include <iostream>2: #include <cstdio>3: #include <cstdlib>4: #include <cstring>5: using namespace std;6:7: #define maxn 128:9: long long f[maxn][1 << maxn];10: int n, m;
11: bool match(int a, int b)12: {13: for (int i = 1; i < 1 << m;)14: {15: if (((a & i) == 0) && ((b & i) == 0))
16: return false;17: if ((a & i) && (b & i))
18: {19: if ((a & (i << 1)) && ((b & (i << 1))))
20: {21: i <<= 2;22: continue;
23: }24: else
25: return false;26: }27: i <<= 1;28: }29: return true;30: }31:32: void work()
33: {34: for (int i = 1; i <= n; i++)35: for (int j = 0; j < 1 << m; j++)36: for (int k = 0; k < 1 << m; k++)37: if (match(k, j))
38: f[i][j] += f[i - 1][k];39: }40:41: int main()
42: {43: while (scanf("%d%d",&n,&m),m!=0)44: {45: memset(f,0,sizeof(f));
46: if (m>n) swap(m,n);
47: f[0][(1 << m) - 1] = 1;48: work();49: printf("%lld\n", f[n][(1 << m) - 1]);
50: }51: //system("pause");
52: return 0;
53: }54: