码迷,mamicode.com
首页 > 其他好文 > 详细

sgu 131 状压DP

时间:2017-04-10 19:46:53      阅读:227      评论:0      收藏:0      [点我收藏+]

标签:ems   table   ber   long   put   other   typedef   cond   others   

棋盘覆盖(二)

时间限制: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

标签:ems   table   ber   long   put   other   typedef   cond   others   

原文地址:http://www.cnblogs.com/--ZHIYUAN/p/6690235.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!