Corn Fields
Time Limit : 4000/2000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 17 Accepted Submission(s) : 13
Farmer John has purchased a lush new rectangular pasture composed of M by N (1 ≤ M ≤ 12; 1 ≤ N ≤ 12) square parcels. He wants to grow some yummy corn for the cows on a number of squares. Regrettably, some of the squares are infertile and can't be planted. Canny FJ knows that the cows dislike eating close to each other, so when choosing which squares to plant, he avoids choosing squares that are adjacent; no two chosen squares share an edge. He has not yet made the final choice as to which squares to plant.
Being a very open-minded man, Farmer John wants to consider all possible options for how to choose the squares for planting. He is so open-minded that he considers choosing no squares as a valid option! Please help Farmer John determine the number of ways he can choose the squares to plant.
Lines 2.. M+1: Line i+1 describes row i of the pasture with N space-separated integers indicating whether a square is fertile (1 for fertile, 0 for infertile)
2 3 1 1 1 0 1 0
9
#include <iostream>
#include <cstdio>
#include <cstring>
#include <bitset>
#include <algorithm>
using namespace std;
#define maxn (1<<12)+5
#define PRIME 100000000
int dp[15][maxn];
int stk[maxn],top;
int M, N;
int a[15];
int OK(int x)
{
if (x&(x << 1))
return 0;
return 1;
}
void FindStatu(int n)
{
top = 0;
for (int i = 0; i < 1 << n;i++)
if (OK(i))
stk[top++] = i;
}
void Solve()
{
FindStatu(M);
memset(dp, 0, sizeof(dp));
for (int i = 0; i < top; i++)
if ((stk[i] & a[0]) == stk[i])
dp[0][stk[i]] = 1;
for (int r = 0; r < N - 1;r++)
for (int i = 0; i < top; i++)
{
if (dp[r][stk[i]])
{
for (int j = 0; j < top;j++)
if ((stk[j] & a[r + 1]) == stk[j])
if (!(stk[j] & stk[i]))
dp[r + 1][stk[j]] += dp[r][stk[i]];
}
}
}
int main()
{
freopen("f:\\input.txt", "r", stdin);
memset(a, 0, sizeof(a));
scanf("%d%d", &N, &M);
for (int i = 0; i < N;i++)
for (int j = 0; j < M; j++)
{
int temp;
scanf("%d", &temp);
a[i] = (a[i] << 1) | temp;
}
Solve();
int ans = 0;
for (int i = 0; i < top; i++)
ans = (ans + dp[N - 1][stk[i]] % PRIME) % PRIME;
printf("%d\n", ans);
return 0;
}