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\n",dp[m+1][0]);
    }
    return 0;
}

 



转载于:https://www.cnblogs.com/--ZHIYUAN/p/6690235.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值