Corn Fields
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 7798 | Accepted: 4159 |
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
Line 1: Two space-separated integers: M and N
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
Line 1: One integer: the number of ways that FJ can choose the squares modulo 100,000,000.
Sample Input
2 3 1 1 1 0 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_back 15 #define ll long long 16 #define mod 100000000 17 int dp[20][1<<13],p[20][20]; 18 int can(int x){// 相邻两位是否相同 19 if(x&(x<<1)) return 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存的是不能放牛的最大状态的值 35 for(int j=0;j<=mmax;j++){ 36 if(j&tmp) continue;//因为j的某些位与tmp中一些位置相同,但是tmp中的是不能放牛的,所以排除 37 if(!can(j)) continue; //有相邻的位置放牛,所以排除 38 for(int k=0;k<=mmax;k++) 39 if(dp[i-1][k]&&!(j&k))//排除与上一行相邻的 40 dp[i][j]=(dp[i][j]+dp[i-1][k])%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 }