您的位置:首页 > 其它

sgu131

2012-04-24 15:04 387 查看

1


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

题目大意:要求在N*M的矩阵中放入1*2的骨牌,或者2*2缺少一个角的骨牌,并且把N*M的矩阵放满,求方案总数

题解:

看到那个羸弱的数据范围,以及棋盘模型,大概都想到要用状态压缩了吧.

这道题可以发现,放入一块骨牌最多只能影响到前面一行,因此我们可以用前面一行的某种状态S2来转移到当前行的状态S1,

这样就可以轻松转移F[i][s1] += f[i-1][s2];

问题是我们如何知道怎样的状态S2能够转移到S2呢,我们可以想到两种方法:

1、先把所有可行状态求出来,用一个数组进行保存

2、每次计算新的一行时,用DFS来重新计算可行状态

第一种方法没有试过,不过看在SGU可怜的内存限制上,我就直接使用了第二种,

这样每次重新DFS计算可行方案即可。

#include<cstdio>
#include<cstring>
const int mn=9;
int ln,n,m;
long long f[2][1<<mn];

void dfs(int p,int s1,int s2,bool b1,bool b2)
{
if(p>m)
{
if(!b1&!b2)f[ln&1][s1]+=f[1-ln&1][s2];
return;
}
if(!b1&!b2)
{
dfs(p+1,s1*2+1,s2*2,0,0);
dfs(p+1,s1*2+1,s2*2,1,0);
dfs(p+1,s1*2+1,s2*2,0,1);
}
if(!b1)
{
dfs(p+1,s1*2+1,s2*2+1-b2,1,0);
dfs(p+1,s1*2+1,s2*2+1-b2,1,1);
}
if(!b2)
dfs(p+1,s1*2+b1,s2*2,1,1);
dfs(p+1,s1*2+b1,s2*2+1-b2,0,0);
}
int main()
{
scanf("%d%d",&n,&m);
if(n<m)n^=m,m=n^m,n^=m;
f[0][(1<<m)-1]=1;
for(ln=1;ln<=n;ln++)
{
dfs(1,0,0,0,0);
memset(f[1-ln&1],0,sizeof(f[0]));
}
printf("%I64d\n",f[n&1][(1<<m)-1]);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: