Poj 3254 corn fields (State compression DP)

Source: Internet
Author: User

Corn fields
Time limit:2000 ms   Memory limit:65536 K
Total submissions:8041   Accepted:4287

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.

Idea: first obtain information that cannot be obtained at the same time in the adjacent lattice, and then remove the information from areas that cannot be planted, and finally obtain the total number of States that do not conflict with the previous line.

# Include "stdio. H "# include" string. H "# include" iostream "# include" algorithm "# include" math. H "# include" vector "using namespace STD; # define ll _ int64 # define n 13 const int mod = 100000000; const int M = 1 <12; int G [N] [N]; int num [m], a [n] [m]; int DP [N] [m]; // store the total number of obtaining methods corresponding to each grid, int n, m, Lim; // The sum of each row is the void Inti () of the answer to the land boundary of this behavior. // initialize the array, the two adjacent grids cannot take {int I, K; Lim = 1 <12; for (I = k = 0; I <Lim; I ++) at the same time) {if (I & (I <1) continue; num [k ++] = I ;}} Int gettmp (int I) // obtain the information of each row. The value of the corresponding bitwise of the undefined lattice is set to {int TMP = 0; For (Int J = 0; j <m; j ++) {TMP <= 1; TMP + = (G [I] [J] ^ 1);} return TMP;} void work () {int I, J, K, TMP; Lim = 1 <m; for (I = 1; I <n; I ++) {for (j = 0; num [J] <Lim; j ++) A [I] [J] = num [J]; TMP = gettmp (I); For (j = 0; num [J] <Lim; j ++) {if (a [I] [J] & TMP) // if it is true, this method cannot obtain the lattice {A [I] [J] = 0; continue;} For (k = 0; num [k] <Lim; k ++) // calculates the number of classes that do not conflict with the previous row. {if (a [I] [J] & A [I-1] [k]) continue; DP [I] [J] + = DP [I-1] [k]; DP [I] [J] % = MOD;} // printf ("% d \ n ", DP [I] [J]) ;}} int ans = 0; for (I = 0; num [I] <Lim; I ++) ans = (ANS + dp [n-1] [I]) % MOD; printf ("% d \ n", ANS) ;}int main () {int I, J; inti (); While (scanf ("% d", & N, & M )! =-1) {Lim = 1 <m; memset (DP, 0, sizeof (DP); for (I = 0; I <n; I ++) for (j = 0; j <m; j ++) scanf ("% d", & G [I] [J]); int TMP = gettmp (0 ); for (I = 0; num [I] <Lim; I ++) {A [0] [I] = num [I]; If (Num [I] & TMP) {DP [0] [I] = 0; A [0] [I] = 0;} else DP [0] [I] = 1;} Work ();} return 0 ;}





Poj 3254 corn fields (State compression DP)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.