Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 23437 | Accepted: 12275 |
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.
Input
Lines 2.. M+1: Line i+1 describes row i of the pasture with N space-separated integers indicating whether a square is fertile (1 for fertile, 0 for infertile)
Output
Sample Input
2 3 1 1 1 0 1 0
Sample Output
9
Hint
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.
1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <algorithm> 5 6 using namespace std; 7 int n,m; 8 const int maxn=13; 9 const int mod=1e8; 10 int arr[15][15]; 11 int state[1<<maxn]; 12 int ee[maxn]; //每一行的状态 13 intDP [MAXN] [ . 1 << MAXN]; // number of i-th row j-th states adopted solution 14 15 16 int main () { . 17 iOS :: sync_with_stdio ( to false ); 18 is DP [ 0 ] [ 0 ] = 1 ; // all not fill to 1 . 19 CIN >> >> n- m; 20 is for ( int I = 1 ; I <= n-; I ++ ) { 21 is for ( int J = 1 ; J <= m; J ++ ) { 22 is CIN >> ARR [I] [J]; 23 is } 24 } 25 int len = . 1 << m; 26 is for ( int I = 0 ; I <len; I ++) { // each state that it will not conflict line 27 State [I] = ((I & (I < < . 1 )) == 0 && (I & (I >> . 1 )) == 0 ); // . 1 means no conflict, the conflict represents 0 28 } 29 for ( int I = . 1 ; I <= n-; I ++ ) { 30 for ( int J = . 1 ; J <= m; J ++ ) { 31 is EE [I] = (EE [I] <<. 1 ) | ARR [I] [J]; // status of each line 32 } 33 is } 34 is for ( int I = . 1 ; I <= n-; I ++ ) { 35 for ( int J = 0 ; J <len; J ++ ) { 36 IF (state [J] && (J & EE [i]) == J) { // first, no conflict followed state to state i rows 37 [ for ( int K = 0 ; K <len; K ++ ) { 38 is IF ((J & K) == 0 ) { // this line and there is no conflict on the column line 39 DP [I] [J] = (DP [I] [J] + DP [I-. 1 ] [K])% MOD; // Do not forget to take the remainder 40 } 41 is } 42 is } 43 is } 44 is } 45 int RES = 0 ; 46 is for ( int I = 0 ; I <len; I ++ ) { 47 RES + = DP [n-] [I]; 48 RES% = MOD; // Do not forget to take the remainder 49 } 50 COUT RES << << endl; 51 is return 0 ; 52 is }