https://www.luogu.com.cn/problem/P1879
Title Description
Farmer John has 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 are infertile and can't be planted. Canny FJ knows that 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.
Farmer John bought a new pasture a new rectangular piece of pasture is divided into M rows and N columns (1 ≤ M ≤ 12; 1 ≤ N ≤ 12), each cell is a square of land. John intends to Gerry planted on a few delicious pasture grass for his cows to enjoy.
Unfortunately, some quite barren land can not be used grass. And, an exclusive piece of grass cows like the feeling, so John would not choose two adjacent land, that is to say, no two have a common edge grass.
John wants to know, if you do not consider the total number of blocks in the grass, then, a total number of species for planting program he chose? (Of course, the new completely abandoned ranch is also a program)
Input Format
The first line: two integers M and N, separated by spaces.
2 through M + 1 rows: each row comprising N integers separated by a space, described in the state of each plot. I + 1-described land line i-th row, are all an integer of 0 or 1, then 1 is, showing sufficient fertile land, 0 indicates not suitable grass land.
Output Format
An integer that ranch allocation scheme is divided by the total number of 100,000,000.
Sample input and output
Input # 1
2 3 1 1 1 0 1 0
Output # 1
9
Obviously, this is like a pressure dp
1, the first pre-grass state i-th row, is an integer compressed state [i].
2, then the status of all non-adjacent pretreatment state judge [i], a total of each row (1 << m) -1 states, but many are present in adjacent cases, the advance state process can not legal state transition in much reduced time complexity .
So how to determine whether a state adjacent to it? Only (i & (i << 1) == 0) && (i & (i >> 1) == 0) can be determined, if the presence of adjacent will judge [i] is set to false, adjacent absent It was true.
3, how to deal with it only into the fertile meadows? For the i-th row terrain State [i] and a state j, if the enumerator to state [i] & j == j illustrate two states are identical i.e., without grass into barren.
4, for the i-th row and i-1 line is not communicating grass, enumerate the program line k, the row j & k == 0 program to satisfy the condition.
1 #include <bits/stdc++.h> 2 #define pb push_back 3 #define fi first 4 #define se second 5 typedef long long LL; 6 const int INF=0x3f3f3f3f; 7 const double eps =1e-8; 8 const int mod=1e8; 9 const int maxn=1e5+10; 10 using namespace std; 11 12 int G[15 ] [ 15 ]; 13 is int State [ 15 ]; // land state i-th row, which is equivalent to 0,1 splicing a [i] [j] in the i-th row with 14 int DP [ 15 ] [ 1 << 12 + 5 ]; // to the i-th row, j is the state of the program number 15 int Judge [ 1 << 12 + 5 ]; // Judge [i] i in this state record is valid 16 int n-, m; . 17 18 is int main () . 19 { 20 is #ifdef the DEBUG 21 is The freopen ( "sample.txt","r",stdin); 22 #endif 23 24 scanf("%d %d",&n,&m); 25 for(int i=1;i<=n;i++) 26 { 27 for(int j=1;j<=m;j++) 28 { 29 scanf("%d",&G[i][j]); 30 state[i]=(state[i]<<1) + G[i][j];//Obtaining the status of each line 31 is } 32 } 33 is for ( int I = 0 ; I <( . 1 << m); I ++) // is determined to meet the requirements of each state 34 is Judge [I] = (I & (I <! < . 1 )) && (I & (I >>! . 1 )); 35 DP [ 0 ] [ 0 ] = . 1 ; 36 for ( int I = . 1 ; I <= n-; I ++) // enumeration each row 37 { 38 is for ( int J = 0 ; J <( . 1 << m); J ++)// Enumeration each possible state of the line 39 { 40 IF (Judge [j] && ((j & state [i]) == j)) // state qualified line, a state j and state [i] is the same ensure the grass in the fertile land 41 is { 42 is for ( int K = 0 ; K <( . 1 << m); K ++ ) 43 is { 44 is IF ((J & K) == 0 ) // this line state on line 1 two states does not appear in the same column of 45 DP [I] [J] = (DP [I] [J] + DP [I- 1 ] [K])% MOD; 46 is } 47 } 48 } 49 } 50 int ans=0; 51 for(int i=0;i<(1<<m);i++) 52 ans=(ans+dp[n][i])%mod; 53 printf("%d\n",ans); 54 55 return 0; 56 }
-