sgu131

5 篇文章 0 订阅
4 篇文章 0 订阅

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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值