Mondriaan's Dream
POJ - 2411
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
1 2 1 3 1 4 2 2 2 3 2 4 2 11 4 11 0 0Sample Output
1 0 1 2 3 5 144 51205
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
long long dp[2050][2050];//注意应该取值范围是2的幂,否则数组越界,一定要使用long long
int m, n;
void dfs(int i, int j, int state, int ne)
{
if(j == n)
{
dp[i+1][ne] += dp[i][state];
return;
}
if(((1<<j)&state)> 0)
dfs( i, j + 1, state, ne);
if(((1 << j)&state) == 0)
dfs( i, j + 1, state, ne|(1<<j));
if(((1 << j)&state) == 0&&((1 << (j+1))&state) == 0&&j+ 1 < n)
dfs( i, j + 2, state, ne);
return;
}
int main()
{
while(scanf("%d %d", &n, &m)!=EOF)
{
if(!n&&!m)
break;
memset(dp,0,sizeof(dp));
dp[1][0] = 1;
for(int i = 1; i <= m; i ++){
for(int j = 0; j < (1 << n); j ++){
if(dp[i][j])
{
dfs(i,0,j,0);
}
}
}
printf("%lld\n", dp[m+1][0]);
}
return 0;
}