用L型和1*2型的地砖铺满n*m的地板统计有多少种方案,用状态压缩解决
一共有六种状态转移,详见代码注释。
131. Hardwood floor
time limit per test: 0.5 sec.
memory limit per test: 4096 KB
The banquet hall of Computer Scientists' Palace has a rectangular form of the size M x N (1<=M<=9, 1<=N<=9). It is necessary to lay hardwood floors in the hall. There are wood pieces of two forms:
1) rectangles (2x1)
2) corners (squares 2x2 without one 1x1 square)
You have to determine X - the number of ways to cover the banquet hall.
Remarks. The number of pieces is large enough. It is not allowed to leave empty places, or to cover any part of a surface twice, or to saw pieces.
Input
The first line contains natural number M. The second line contains a natural number N.
Output
First line should contain the number X, or 0 if there are no solutions.
Sample Input
2 3
Sample Output
5
#include<iostream>
#include<cstring>
#include<cstdio >
#include<string>
#include<algorithm>
using namespace std;
typedef long long LL;
int m,n;
LL f[1<<11],dp[1<<11];
//检查状态s的第i位是否能够填放
bool jud(int s,int i)
{
return (s&(1<<i))==0;
}
void dfs(int pre,int cur,LL cnt)
{
if(pre==(1<<m)-1) //第i行已满,cur为对应的i+1行状态
{
dp[cur]+=cnt;
return;
}
else
{
for(int i=0;i<m;i++)
{
if(!jud(pre,i)) continue;
//##
//..
if(i+1<m && jud(pre,i+1))
{
int state=pre|(1<<(i+1));
state|=(1<<i);
dfs(state,cur,cnt);
}
//#.
//#.
if(jud(cur,i))
{
int state1=pre|(1<<i);
int state2=cur|(1<<i);
dfs(state1,state2,cnt);
}
//#.
//##
if(i+1<m&&jud(cur,i)&&jud(cur,i+1))
{
int state1=pre|(1<<i);
int state2=cur|(1<<i);
state2|=(1<<(i+1));
dfs(state1,state2,cnt);
}
//.#
//##
if(i>=1&&jud(cur,i)&&jud(cur,i-1))
{
int state1=pre|(1<<(i));
int state2=cur|(1<<i);
state2=state2|(1<<(i-1));
dfs(state1,state2,cnt);
}
//##
//#.
if(i+1<m&&jud(pre,i+1)&&jud(cur,i))
{
int state1=pre|(1<<i);
state1=state1|(1<<(i+1));
int state2=cur|(1<<i);
dfs(state1,state2,cnt);
}
//##
//.#
if(i+1<m&&jud(pre,i+1)&&jud(cur,i+1))
{
int state1=pre|(1<<i);
state1=state1|(1<<(i+1));
int state2=cur|(1<<(i+1));
dfs(state1,state2,cnt);
}
break;// 防止重复计数,先放后方没有区别,所以每次DFS只转移一块地板的状态
}
}
}
int main()
{
cin>>n>>m;
int tot=(1<<m);
f[tot-1]=1;
for(int i=0;i<=n;i++) //表示第i行已经满,求出对应下一行各种状态及方案
{
for(int s=0;s<tot;s++) //枚举i行初始状态,求出相应i+1行的状态
if(f[s]) dfs(s,0,f[s]); //dfs(i行初始状态i’,i’对应的i+1行状态,转移的方案数)
for(int s=0;s<tot;s++) //滚动数组的作用。
{
f[s]=dp[s];
dp[s]=0;
}
}
cout<<f[0]<<endl; //第n行铺满,第n+1行0;
return 0;
}