Online questions:
Corn fields
| Time limit:2000 ms |
|
Memory limit:65536 K |
| Total submissions:6936 |
|
Accepted:3697 |
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. in an N * M matrix, some grids are normal, and some are broken. Now we need to put a good grid on it, the principle is that the number of different schemes cannot be equal to the number of workers in an adjacent grid. (The number of schemes for different cattle is also a solution ). For the first time, the problem of State compression cannot be well solved for the moment. We need to analyze the practice. For a column, if all the grids are in good condition, there are only two methods that we can put the most, because they need to be opened separately. At the same time, if we need to use binary to represent all States of a row, the number of States will not be much, just use an int32, here, we only need to use an array res [] to record the maximum value of a certain state of the current row before the I line, the condition that a certain State in the previous row is transferred to the current state is that there is no conflict between the two methods. Code:
1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 #define MAX 2048 5 #define LL long long 6 #define MOD 100000000 7 using namespace std; 8 9 LL dp[15][MAX];10 LL in[MAX],state[MAX];11 int m,n,cnt;12 13 void init(){14 cnt=0;15 for(int i=0;i<(1<<n);i++) if( (i&(i<<1)) == 0 ) state[cnt++]=i;16 }17 18 int main()19 {20 //freopen("data.txt","r",stdin);21 while(scanf("%d %d",&m,&n)!=EOF){22 init();23 for(int i=0;i<m;i++){24 int o;25 in[i]=0;26 for(int j=0;j<n;j++){27 scanf("%d",&o);28 in[i]=in[i]|(o<<j);29 }30 }31 memset(dp,0,sizeof(dp));32 for(int i=0;i<cnt;i++){33 if( (in[0]&state[i]) == state[i] ) dp[0][i]=1;34 }35 for(int i=1;i<m;i++){36 for(int j=0;j<cnt;j++){37 if( (in[i]&state[j]) == state[j] )38 for(int k=0;k<cnt;k++){39 if( (state[j]&state[k]) == 0) dp[i][j]=(dp[i][j]+dp[i-1][k])%MOD;40 }41 }42 }43 LL sum=0;44 for(int i=0;i<cnt;i++) sum=(sum+dp[m-1][i])%MOD;45 cout<<sum<<endl;46 }47 return 0;48 }3254