Corn fields
Time limit:2000ms Memory limit:65536k
Total submissions:16318 accepted:8615
Description
Farmer John had 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 is infertile and can ' t be planted. Canny FJ knows that the cows dislike eating close to all other, so when choosing which squares to plant, he avoids choosi Ng squares that is adjacent; No. 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 what to choose the squares for Plantin G. He's so open-minded that he considers choosing no squares as a valid option! Farmer John determine the number of ways he can choose the squares to plant.
Input
Line 1:two space-separated integers:m and N
Lines 2..m+1:line i+1 describes row I of the pasture with N space-separated integers indicating whether a square is Ferti Le (1 for fertile, 0 for infertile)
Output
Line 1:one integer:the number of ways that FJ can choose the squares modulo 100,000,000.
Sample Input
2 3
1 1 1
0 1 0
Sample Output
9
Hint
Number the squares as follows:
1 2 3
4
There is four ways to plant only on one squares (1, 2, 3, or 4), three ways to plant on both squares (, or), 1 WA Y to plant on three squares (134), and one-to-plant on no squares. 4+3+1+1=9.
Source
Usaco 2006 November Gold
#include <stdio.h> #include <string.h> #include <algorithm> using namespace std;
const int MOD = (int) 1e9;
int dp[12][1<<10], ste[1<<10], SZ, fid[12], N, m;
void Slove () {sz = 0;
for (int i = 0; i < 1<<m; ++i) {if (i&i<<1) continue; if (! (
I&fid[0])) Dp[0][sz] = 1;
ste[sz++] = i;
} for (int i = 1, i < n; ++i) {for (int j = 0; j < sz; ++j) {if (ste[j]&fid[i]) continue;
for (int k = 0; k < sz; ++k) {if (ste[j]&ste[k]) continue;
DP[I][J] + = dp[i-1][k];
DP[I][J]%= MOD;
}}} int ans = 0;
for (int i = 0; i < sz; ++i) {ans + = dp[n-1][i];
Ans%= MOD;
} printf ("%d\n", ans);
} int main () {scanf ("%d%d", &n, &m);
for (int i = 0, i < n; ++i) {for (int j = 0; j < m; ++j) {int x; scanf ("%d", &x);
if (!x) fid[i] + = 1<<j;
}} slove ();
return 0;
}
To refer to http://blog.csdn.net/accry/article/details/6607703