https://cn.vjudge.net/problem/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
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
在位置(i, j) 如果我们选择横着贴砖,那么将(i, j), (i, j+1)都填写成1, 如果竖着贴砖,我们将(i,j)填写成0, 将(i+1, j)填写成1
详见https://blog.csdn.net/u014634338/article/details/50015825
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<map>
#include<iostream>
#pragma GCC optimize(3)
#define max(a,b) a>b?a:b
using namespace std;
typedef long long ll;
ll dp[15][1<<12];
int n,m;
bool first(int sta){ //判断符不符合第一行的状态
for(int j=0;j<m;){
if(sta&(1<<j)){
if(j==m-1) return false;
if(sta&(1<<(j+1))) j+=2;
else return false;
}
else j++;
}
return true;
}
bool judge(int now_sta,int pre_sta){
for(int j=0;j<m;){
if(now_sta&(1<<j)){
if(pre_sta&(1<<j)){
if(j==m-1||(now_sta&(1<<(j+1)))==0||(pre_sta&(1<<(j+1)))==0)
return false;//上下应该都是横放
else j+=2;
}
else j++;//上0下1,竖放
}
else{ //下面是0,上面不能是0
if(pre_sta&(1<<j)) j++;
else return false;
}
}
return true;
}
int main(){
while(~scanf("%d%d",&n,&m)){
if(n==0&&m==0) break;
if((n*m)&1) printf("0\n");
else{
if(n<m) swap(n,m);
int up=1<<m;
for(int i=1;i<=n;i++){
for(int j=0;j<up;j++){
dp[i][j]=0;
}
}
for(int i=0;i<up;i++){
if(first(i)) dp[1][i]=1;
}
for(int i=2;i<=n;i++){
for(int j=0;j<up;j++){//当前行状态
for(int k=0;k<up;k++){//上一行状态
if(judge(j,k)) dp[i][j]+=dp[i-1][k];
}
}
}
printf("%lld\n",dp[n][up-1]);
}
}
return 0;
}