您的位置:首页 > 其它

SGU 131 Hardwood floor(状压DP)

2015-01-14 14:56 337 查看


131. Hardwood floor

time limit per test: 0.25 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

思路 : dp[ i ][ j ] 表示第 i 行状态为 j 的方案数 。

dfs(x,y,s1,s2,b1,b2)枚举状态(x为当前行号,p为当前列号,s1、s2当前行和上

一行的覆盖情况,b1、b2上一列的放置对当前列两行的影响,影响为1否则为0。初

始时s1=s2=b1=b2=0。)

下图是dfs参数变化的情况。



#include <iostream>
#include <cstdio>
#include <cstring>
#define LL long long
using namespace std;
const int N=9;

LL dp[11][1<<N],cnt;
int n,m,len;

void dfs(int x,int y,int s1,int s2,int b1,int b2)
{
if(y>m)
{
if(!b1 && !b2)   dp[x][s1]+=dp[x-1][s2];
return ;
}
if(!b1 && !b2)
{
dfs(x,y+1,s1*2+1,s2*2,0,0);
dfs(x,y+1,s1*2+1,s2*2,1,0);
dfs(x,y+1,s1*2+1,s2*2,0,1);
}
if(!b1)
{
dfs(x,y+1,s1*2+1,s2*2+1-b2,1,0);
dfs(x,y+1,s1*2+1,s2*2+1-b2,1,1);
}
if(!b2)  dfs(x,y+1,s1*2+b1,s2*2,1,1);
dfs(x,y+1,s1*2+b1,s2*2+1-b2,0,0);
}

int main()
{
scanf("%d %d",&n,&m);
len=1<<m;
memset(dp,0,sizeof(dp));
dp[0][len-1]=1;
for(int i=1; i<=n; i++)   dfs(i,1,0,0,0,0);
cout<<dp
[len-1]<<endl;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: