luogu P1879 [USACO06NOV]Corn Fields G 状态压缩dp

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
const int N=15,M=1<<N;
const int mod=100000000;
int n,m;
int f[N][M],g[N][N];
vector<int>state[N];
bool check(int k,int state)
{
	for(int i=0; i<m; i++)
		if( ((state>>i&1 && state>>i+1&1))||((state>>i&1 && !g[k][m-i])))
			return false;
	return true;
}
int main()
{
	cin>>n>>m;
	for(int i=1; i<=n; i++)
		for(int j=1; j<=m; j++)
			cin>>g[i][j];
	for(int i=0; i< 1<<m; i++)
		if(check(1,i))
		{
			state[1].push_back(i);
			f[1][i]=1;
		}
	for(int i=2; i<=n+1; i++)
		for(int j=0; j< 1<<m; j++)
		{
			if(!check(i,j))
				continue;
			state[i].push_back(j);
			for(auto k : state[i-1])
			{
				if(k&j)
					continue;
				f[i][j]=(f[i][j]+f[i-1][k])%mod;
			}
		}
	cout<<f[n+1][0]<<endl;
	return 0;
}
posted @ 2020-05-14 13:19  晴屿  阅读(98)  评论(0编辑  收藏  举报