zoukankan      html  css  js  c++  java
  • sgu 131 状压DP

    棋盘覆盖(二)

    时间限制:1000 ms  |  内存限制:65535 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.
     
    输入
    The first line contains natural number M. The second line contains a natural number N. 

    输出
    First line should contain the number X, or 0 if there are no solutions.
    样例输入
    2 3
    
    样例输出
    5

    题意:
    有1*2的砖块和2*2但缺一个角的砖块,用他们恰好铺满m*n的矩形有几种方案。
    代码:
    //思路就是:状压,从上到下铺dfs,sta表示本行的状态,next表示下一行的状态,每次要把sta铺满
    //,next随之变化。最后要m+1行的状态为0才可以。
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    using namespace std;
    typedef long long ll;
    ll dp[11][1<<10];
    int n,m;
    bool chak(int sta,int x){
        int tmp=(sta&(1<<x));
        return !tmp;
    }
    void dfs(int sta,int next,int col,int row,ll w){
        if(col==n){
            dp[row+1][next]+=w;
            return;
        }
        if(sta&(1<<col)) dfs(sta,next,col+1,row,w); //不放
        else{
            int st=(sta|(1<<col));
            if(chak(next,col)){
                dfs(sta|(1<<col),next|(1<<col),col+1,row,w);//竖着的1*2
                if(col+1<n&&chak(sta,col+1))
                    dfs(st|(1<<(col+1)),next|(1<<col),col+1,row,w);//缺右上角的2*2
                st=(next|(1<<col));
                if(col+1<n&&chak(next,col+1))
                    dfs(sta|(1<<col),st|(1<<(col+1)),col+1,row,w);//缺右下角的2*2
                if(col-1>=0&&chak(next,col-1))
                    dfs(sta|(1<<col),st|(1<<(col-1)),col+1,row,w);//缺左下角的2*2
            }
            st=(sta|(1<<col));
            if(col+1<n&&chak(sta,col+1)){
                dfs(st|(1<<(col+1)),next,col+1,row,w);//横着的1*2
                if(chak(next,col+1))
                    dfs(st|(1<<(col+1)),next|(1<<(col+1)),col+1,row,w);//缺左上角的2*2
            }
        }
    }
    int main()
    {
        while(scanf("%d%d",&m,&n)==2){
            memset(dp,0,sizeof(dp));
            dp[1][0]=1;
            int N=(1<<n);
            for(int i=1;i<=m;i++){
                for(int j=0;j<N;j++){
                    if(dp[i][j])
                        dfs(j,0,0,i,dp[i][j]);
                }
            }
            printf("%lld
    ",dp[m+1][0]);
        }
        return 0;
    }


  • 相关阅读:
    知识点整理
    NGINX 内存池有感
    NGINX怎样处理惊群的
    NGINX 定时器
    制作linux内核安装包
    ES6变量的解构赋值
    jquery uploadify上传插件用法心得
    【转贴】J2EE中的13种技术规范
    【转帖】Servlet 3.0 新特性详解
    汉诺塔问题的一个C#实现
  • 原文地址:https://www.cnblogs.com/--ZHIYUAN/p/6690235.html
Copyright © 2011-2022 走看看