2016夏季练习——dp

来源:POJ3254

状压dp

解释见注释

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int N = 15;
const int MAXN = (1<<12)+10;
const int MOD = 100000000;
int mp[N];
int dp[N][MAXN];//dp[floor][state] means the max in the map dp[floor][state]
int n,m;
int st[MAXN];
int main(){
	while(scanf("%d%d",&n,&m)!=EOF){
		memset(mp,0,sizeof(mp));
		memset(dp,0,sizeof(dp));
		int du;
		for(int i=1;i<=n;i++){
			for(int j=1;j<=m;j++){
				scanf("%d",&du);
				if(du==0){
					mp[i]+=(1<<(j-1));//store the map
					//mp[i]=st
					//i means the ith floor
					//st means a state
				}
			}
		}
		int k=0;
		for(int i=0;i<(1<<m);++i){
			//we consider all the state
			//because the number is m
			//so we have (1<<m) to present all the statement
			if(!(i&(i<<1))){//we judge all the state whether there is a adjacent
				st[k++]=i;
				//get all the state which is satisfying
			}
		}
		//now we get the first floor answer

		for(int i=0;i<k;i++){
			if(!(mp[1]&st[i])){
				dp[1][i]=1;
			}
		}

		//we start to dp
		for(int i=2;i<=n;i++){
			for(int j=0;j<k;j++){
				if(mp[i]&st[j]) continue;
				for(int h=0;h<k;h++){
					if(mp[i-1]&st[h]) continue;
					if(!(st[h]&st[j])){
						dp[i][j]+=dp[i-1][h];
					}
				}
			}
		}
		int ans=0;
		for(int i=0;i<k;i++){
			ans+=dp[n][i];
			ans%=MOD;
		}
		cout<<ans<<endl;
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值