来源: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;
}