POJ 2411 Mondriaan's Dream 状压dp

题目链接:

https://vjudge.net/contest/159644#problem/G

题意:

用1*2的砖去恰好铺满n*m的空间,有多少种方法

题解:

http://blog.csdn.net/xingyeyongheng/article/details/21692655

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long ll;
#define MS(a) memset(a,0,sizeof(a))
#define MP make_pair
#define PB push_back
const int INF = 0x3f3f3f3f;
const ll INFLL = 0x3f3f3f3f3f3f3f3fLL;
inline ll read(){
    ll x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
//////////////////////////////////////////////////////////////////////////
const int maxn = (1<<11)+10;

int n,m;
ll cnt[maxn],dp[maxn];
bool mark[maxn];

bool check(int x){
    while(x){
        if(x&1){
            x >>= 1;
            if(!(x&1)) return false;
            x >>= 1;
        }else {
            x >>= 1;
        }
    }
    return true;
}

void init(){
    MS(mark); MS(cnt);
    for(int i=0; i<(1<<m); i++){
        if(check(i))
            cnt[i]=1,mark[i]=true;
    }
}

void DP(){
    for(int k=2; k<=n; k++){
        MS(dp);
        for(int i=0; i<(1<<m); i++){ // 枚举本行的状态
            for(int j=0; j<(1<<m); j++){  // 枚举上一行的状态,找到可以转移到第i行的状态
                if((i|j) != (1<<m)-1) continue;
                if(!mark[i&j]) continue;
                dp[i] += cnt[j]; //i可以从j到达,则增加j的方案数
// mark[i&j] 是为了判断两行都是横着放的状态--->  1 1
//                                               1 1
// 如果是 i = 00110011
//        j = 11011111  i&j=00010011 这是不合法的

            }
        }
        for(int i=0; i<(1<<m); i++) cnt[i] = dp[i];
    }
}

int main(){
    while(cin>>n>>m && (n+m)){
        if(n<m) swap(n,m); //始终保持m<n,提高效率   
        init();
        DP();

        cout << cnt[(1<<m)-1] << endl; //输出最后一行到达时的状态必须全部是1
    }

    return 0;
}
// http://blog.csdn.net/xingyeyongheng/article/details/21692655
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值