[POJ2411]Mondriaan’s Dream铺砖块
传送门POJ2411
Description
现有n* m的一块地板,需要用1*2的砖块去铺满,中间不能留有空隙。问这样方案有多少种
Input
输入n,m(1<=n, m<=11)
有多组输入数据,以m=n=0结束
Output
输出铺砖块的方案数
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
Solution
- 对于每一格有横放或者竖放,且由数据范围,想到状压DP
- dfs预处理,用p数组记录相邻两行的合法状态
- 转移:f[i][p[j][1]]+=f[i-1][p[j][0]];
Code
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#define ll long long
using namespace std;
int p[50007][2],tot,n,m;
ll f[17][50007];
void dfs(int k,int now,int pre){
if (k>m) return;
if (k==m){
p[++tot][0]=now;
p[tot][1]=pre;
return;
}
dfs(k+1,(now<<1)+1,pre<<1);
dfs(k+1,now<<1,(pre<<1)+1);
dfs(k+2,(now<<2),(pre<<2));
}
int main(){
while (true){
scanf("%d%d",&n,&m);
if (n==0&&m==0) break;
memset(f,0,sizeof(f));
tot=0; dfs(0,0,0);
f[0][0]=1ll;
for (int i=1;i<=n;i++)
for (int j=1;j<=tot;j++)
f[i][p[j][1]]+=f[i-1][p[j][0]];
printf("%lld\n",f[n][0]);
}
}
喵喵喵
以上です