SGU 131 Hardwood floor(狀壓DP)

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[n][len-1]<<endl;
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章