您的位置:首页 > 其它

POJ 2411 Mondriaan's Dream 状压dp

2017-04-21 22:00 85 查看

题目链接:

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[/code] 
                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: