Poj 3254 Corn Fields

一个简单的状态压缩DP


按行压缩按列压缩都可以


会状压DP了这个就是水题


#include<cstdio>
#include<cstring>
using namespace std;

#define LL long long
const LL mod = 100000000;

const int Log = 13;
const int maxn = 1<<Log;

LL dp[Log][maxn];

int bmap[Log];

int map[Log][Log];

bool able(int x){
    return (x & (x<<1)) == 0;
}

int main(){
	int n,m;
	while(~scanf("%d %d",&n,&m)){
		for(int i=0;i<n;i++){
			for(int j=0;j<m;j++){
				scanf("%d",&map[i][j]);
			}
		}
		for(int i=0;i<n;i++){
			bmap[i] = 0;
			for(int j=0;j<m;j++){
				bmap[i] = bmap[i]*2 + (1-map[i][j]);
			}
		}
		memset(dp,0,sizeof(dp));
		int len = 1<<m;
		for(int i=0;i<len;i++){
			if( (i&bmap[0]) == 0 && able(i)){
				dp[0][i] = 1;
			}
		}
		for(int i=1;i<n;i++){
            for(int j = 0;j<len;j++){
                if((j&bmap[i])==0 && able(j)){
                    for(int bj = 0;bj<len;bj++){
                        if( (bj&j)==0 && able(bj))
                            (dp[i][j] += dp[i-1][bj]) %= mod;
                    }
                }
			}
		}
		LL ans = 0;
		for(int i=0;i<len;i++){
			(ans += dp[n-1][i]) %= mod;
		}
		printf("%lld\n",ans);
	}
	return 0;
}


你可能感兴趣的:(Poj 3254 Corn Fields)