您的位置:首页 > 其它

sgu131 Hardwood floor

2014-10-15 22:01 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。由于这两种放法会占用下一填充块的下方位置。所以边界需将当前填充块的下方考虑进去。 压缩状态时加一位,表示该位置。



代码:
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
long long  dp[2][1500];
int main()
{
int m,n,cnt,i,j,k;
scanf("%d%d",&m,&n);
memset(dp,0,sizeof(dp));
cnt=0;
dp[0][0]=1;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
memset(dp[cnt^1],0,sizeof(dp[cnt^1]));
for(k=0;k<1<<(n+1);k++)
{
if(dp[cnt][k]==0)   continue;
if(k>>j&1)
{
if(k>>n&1)
dp[cnt^1][k&~(1<<n)]+=dp[cnt][k];
else
dp[cnt^1][k&~(1<<j)]+=dp[cnt][k];
}
else
{
//1*2砖块
if(j<n-1&&!(k>>(j+1)&1))
{
if(k>>n&1)
dp[cnt^1][k&(~(1<<n))|(1<<(j+1))|1<<j]+=dp[cnt][k];

else
dp[cnt^1][k|(1<<(j+1))]+=dp[cnt][k];
}
if(i<m-1&&!(k>>n&1))
dp[cnt^1][k|(1<<j)]+=dp[cnt][k];

//3砖块
if(j<n-1&&i<m-1&&!(k>>(j+1)&1)&&!(k>>n&1))
dp[cnt^1][k|(1<<j)|(1<<(j+1))]+=dp[cnt][k];

if(j<n-1&&i<m-1&&!(k>>n&1))
dp[cnt^1][k|(1<<j)|(1<<n)]+=dp[cnt][k];

if(j>0&&i<m-1&&!(k>>(j-1)&1)&&!(k>>n&1))
dp[cnt^1][k|(1<<(j-1))|(1<<j)]+=dp[cnt][k];

if(j<n-1&&i<m-1&&!(k>>(j+1)&1))
{
if(k>>n&1)
dp[cnt^1][k|(1<<j)|1<<(j+1)]+=dp[cnt][k];
else
dp[cnt^1][k|(1<<(j+1)|(1<<n))]+=dp[cnt][k];
}
}
}
cnt^=1;
}
}
printf("%lld\n",dp[cnt][0]);
return 0;
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: