sgu 131 状压DP

Posted LuZhiyuan

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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铺满
//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\n",dp[m+1][0]);
    }
    return 0;
}

 



以上是关于sgu 131 状压DP的主要内容,如果未能解决你的问题,请参考以下文章

Be a Smart Raftsman SGU475

HDU4057 Rescue the Rabbit(AC自动机+状压DP)

18.06.03 POJ 4126:DNA 15年程设期末05(状压DP)

POJ1699 Best Sequence(AC自动机+状压DP)

今日SGU 5.15

SGU495 概率DP