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