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

      

  • 相关阅读:
    互斥锁和条件变量实现生产者消费者问题
    信号量实现生产者消费者问题
    IPC进程间通信---共享内存
    IPC进程间通信---消息队列
    图的遍历---广度优先遍历和深度优先遍历
    图的两种存储方式---邻接矩阵和邻接表
    内存分配---FF、BF、WF三种算法
    C++的前置++、后置++和前置--、后置--
    IPC进程间通信---信号量
    Linux进程间通信---管道和有名管道
  • 原文地址:https://www.cnblogs.com/xuesu/p/4023446.html
Copyright © 2020-2023  润新知