Getting started with poj 3250 status compression DP

Source: Internet
Author: User

 

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

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.