SGU 131. Hardwood floor 状压dp 难度:2

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
不知不觉就和nocow的题解很像了,一开始想直接把两行全部加入状态,但是使得转移很麻烦
#include<cstdio>
#include <cstring>
using namespace std;
long long  thline[1<<10],nxtline[1<<10];
#define full ((1<<(m))-1)
#define bin(x) (1<<(x))
#define empt(x,l) ((x)&bin(l))
int n,m;
void dfs(int thsl,int nxtl,long long addnum){
    if(thsl==full){//if dye this line all with the effect of nxtl
        nxtline[nxtl]+=addnum;
        return ;
    }
    for(int i=0;i<m;i++){
        if(empt(thsl,i)==0){
            /*##*/
            if(i<m-1&&empt(thsl,i+1)==0){
                dfs(thsl|bin(i)|bin(i+1),nxtl,addnum);
            }
            /*#
              #*/
            if(empt(nxtl,i)==0){
                dfs(thsl|bin(i),nxtl|bin(i),addnum);
            }
            /*##
              #*/
            if(i<m-1&&empt(nxtl,i)==0&&empt(thsl,i+1)==0){
                dfs(thsl|bin(i)|bin(i+1),nxtl|bin(i),addnum);
            }
            /*##
              #*/
            if(i<m-1&&empt(nxtl,i+1)==0&&empt(thsl,i+1)==0){
                dfs(thsl|bin(i)|bin(i+1),nxtl|bin(i+1),addnum);
            }
            /*#
             ##*/
            if(i&&empt(nxtl,i)==0&&empt(nxtl,i-1)==0){
                dfs(thsl|bin(i),nxtl|bin(i-1)|bin(i),addnum);
            }
            /*#
              ##*/
            if(i<m-1&&empt(nxtl,i+1)==0&&empt(nxtl,i)==0){
                dfs(thsl|bin(i),nxtl|bin(i)|bin(i+1),addnum);
            }
            break;//dye only one block everytime to avoid empty
        }
    }
}
int main(){
    scanf("%d%d",&n,&m);
    thline[full]=1;
    for(int i=0;i<=n;i++){//add a zero line which isnt exist,then it will look like
        /*####
          &&&&(&means empty)
        */
        for(int j=0;j<=full;j++){
            if(thline[j])dfs(j,0,thline[j]);
        }
        memcpy(thline,nxtline,sizeof(nxtline));
        memset(nxtline,0,sizeof(nxtline));
    }
    printf("%I64d\n",thline[0]);
    return 0;
}

  

posted @ 2014-10-14 00:47  雪溯  阅读(298)  评论(0编辑  收藏  举报