Corn fields
Time limit:2000 ms |
|
Memory limit:65536 K |
Total submissions:8062 |
|
Accepted:4295 |
Description
Farmer John has purchased a lush New Rectangular pasture composedMByN(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. 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.
Input
Line 1: two space-separated integers:
MAnd
N
Lines 2 ..
M+ 1: Line
I+ 1 describes row
IOf the pasture
NSpace-separated integers indicating whether a square is fertile (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 31 1 10 1 0
Sample output
9
Hint
Number the squares as follows:
1 2 3 4
There are four ways to plant only on one squares (1, 2, 3, or 4), three ways to plant on two squares (13, 14, or 34 ), 1 way to plant on three squares (134), and one way to plant on no squares. 4 + 3 + 1 + 1 = 9.
Source
Usaco 2006 November gold
Solution:
The rectangle with N rows and M columns is divided into N * m grids, each of which is marked as 0 or 1. Put a bull in these grids, where 1 indicates that the grid can be placed with cows, 0 indicates that a cow cannot be placed, and two adjacent grids cannot have cows at the same time. Ask the total number of solutions.
The concept is state compression. The status of each row is regarded as a binary number. DP [I] [J] indicates the number of solutions that are shared by the first I row when the status of row I is J.
Code:
# Include <iostream> # include <string. h> # include <algorithm> using namespace STD; const int mod = 100000000; const int maxn = 12; int DP [maxn + 1] [(1 <maxn) + 1]; int num [maxn + 1]; int n, m; bool check (int I, int X) // check whether status X of row I is valid {If (X & num [I])! = X) // It is clever to judge whether the status X of row I is valid. Why is it so written, because 0 cannot be used in place, // The valid status and the original status are 0 bits, and the original status 1 bits correspond to the valid status bits and are equal to the valid status bits return 0; if (X & (x> 1) | X & (x <1) // There cannot be two adjacent 1 return 0; return 1 ;}int main () {CIN> N> m; int X; memset (Num, 0, sizeof (Num); memset (DP, 0, sizeof (DP )); for (INT I = 1; I <= N; I ++) for (Int J = 1; j <= m; j ++) {CIN> X; if (x) num [I] = num [I] | (1 <(J-1 )); // Save the status of each row to num [I]} int max = (1 <m); DP [0] [0] = 1; // pay attention to this sentence! For (INT I = 1; I <= N; I ++) // enumerate each row {for (Int J = 0; j <Max; j ++) {If (! Check (I, j) continue; For (int K = 0; k <Max; k ++) if (J & K) = 0) {DP [I] [J] + = DP [I-1] [k]; If (DP [I] [J]> = mod) DP [I] [J] % = mod ;}} int ans = 0; For (INT I = 0; I <Max; I ++) {ans + = DP [N] [I]; If (ANS> = mod) ans % = MOD;} cout <ans; return 0 ;}
[ACM] poj 3254 corn fields (State compression)