Corn fields
Time limit:2000 ms |
|
Memory limit:65536 K |
Total submissions:7798 |
|
Accepted:4159 |
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
1 # include <iostream> 2 # include <string> 3 # include <cstdio> 4 # include <vector> 5 # include <queue> 6 # include <stack> 7 # include <set> 8 # include <algorithm> 9 # include <cstring> 10 # include <stdlib. h> 11 # include <math. h> 12 # include <map> 13 using namespace STD; 14 # define Pb push_back15 # define ll long long16 # define mod 10000000017 int DP [20] [1 <13], P [20] [20]; 18 int can (int x) {// whether the adjacent two digits are the same 19 if (X & (x <1) retur N 0; 20 else return 1; 21} 22 int main () {23 int n, m; 24 while (CIN> N> m) {25 int mMax = (1 <m)-1; 26 for (INT I = 1; I <= N; I ++) 27 for (Int J = 1; j <= m; j ++) 28 CIN> P [I] [J]; 29 memset (DP, 0, sizeof (DP )); 30 DP [0] [0] = 1; 31 for (INT I = 1; I <= N; I ++) {32 int TMP = 0; 33 for (Int J = 1; j <= m; j ++) 34 if (! P [I] [J]) TMP + = (1 <(J-1 )); // TMP stores 35 for (Int J = 0; j <= mMax; j ++) {36 IF (J & TMP) continue; // because some bits of J are in the same position as TMP, but the bits in TMP cannot be placed, 37 If (! Can (j) continue; // place a cow in an adjacent position, so 38 for (int K = 0; k <= mMax; k ++) is excluded) 39 if (DP [I-1] [k] &! (J & K) // exclude 40 DP [I] [J] = (DP [I] [J] + dp [I-1] [k]) adjacent to the previous line % MOD; 41} 42} 43 ll ans = 0; 44 for (INT I = 0; I <= mMax; I ++) ans = (ANS + dp [N] [I]) % MOD; 45 cout <ans <Endl; 46} 47}
Getting started with poj 3250 status compression DP