sgu131
来源:互联网 发布:网络机柜厂商 编辑:程序博客网 时间:2024/06/17 00:39
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;}
- sgu131
- sgu131
- sgu131
- SGU131--NYOJ435
- SGU131 Hardwood floor
- sgu131 Hardwood floor
- sgu131:Hardwood floor
- SGU131 Hardwood floor
- sgu131 Hardwood floor 状压DP
- SGU131 状态压缩~覆盖方案数
- 状态压缩棋盘问题2道 sgu131&poj1038
- NYOJ435 & SGU131 - 棋盘覆盖二 (状压DP+插头DP)
- httpclient-4.1.3结合dom4j的完整演示实例
- NYOJ 61 传纸条
- 【Android】ADT-16升级至18出现的问题
- 如何判断一棵树是否为二叉排序树 C版
- android自定义tabwidget,popupwindow,spinner,dialog,listview
- sgu131
- 试卷
- 英语课演讲感受
- 学习OpenCV——KNN算法
- 2012年4月20日 最新长尾关键字优化
- php Zend framework 学习
- NP和P问题
- java例程练习(Point类及简单测试)
- 判断32位整数二进制中1的个数