状压DP POJ 3254 Corn Fields

时间:2022-05-22 12:27:57

题目传送门

 /*
状态压缩DP:先处理硬性条件即不能种植的,然后处理左右不相邻的,
接着就是相邻两行查询所有可行的种数并累加 写错一个地方差错N久:)
详细解释:http://www.tuicool.com/articles/JVzMVj
*/
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string>
#include <map>
#include <set>
using namespace std; const int MAXN = ;
const int INF = 0x3f3f3f3f;
const int MOD = 1e8;
int dp[MAXN][ << MAXN];
int st[ << MAXN];
int rem[ << MAXN]; int solve(int m)
{
int cnt = ;
int tot = << m;
for (int i=; i<tot; ++i)
{
if ((i & (i << )) == )
{
st[++cnt] = i;
}
} return cnt;
} void work(int n, int m, int cnt)
{
memset (dp, , sizeof (dp));
for (int i=; i<=cnt; ++i)
{
if ((st[i] & rem[]) == )
{
dp[][i] = ;
}
} for (int i=; i<=n; ++i)
{
for (int k=; k<=cnt; ++k)
{
if ((st[k] & rem[i]) == )
{
for (int j=; j<=cnt; ++j)
{
if (((st[j] & rem[i-]) == ) && ((st[j] & st[k]) == ))
dp[i][k] = (dp[i][k] + dp[i-][j]) % MOD;
}
}
}
} int ans = ;
for (int i=; i<=cnt; ++i)
{
ans = (ans + dp[n][i]) % MOD;
} printf ("%d\n", ans);
} int main(void) //POJ 3254 Corn Fields
{
#ifndef ONLINE_JUDGE
freopen ("F.in", "r", stdin);
#endif int n, m;
while (~scanf ("%d%d", &n, &m))
{
int cnt = solve (m); memset (rem, , sizeof (rem));
for (int i=; i<=n; ++i)
{
int x;
for (int j=; j<=m; ++j) //看错n和m
{
scanf ("%d", &x);
if (x == )
{
rem[i] = rem[i] | ( << (m-j));
}
}
} work (n, m, cnt);
} return ;
}