Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 14326 | Accepted: 7509 |
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.
Source
状压DP
不能选相邻的格子。先预处理出所有可能的状态(其实没必要),枚举状态转移,累计方案数
1 /*by SilverN*/ 2 #include<algorithm> 3 #include<iostream> 4 #include<cstring> 5 #include<cstdio> 6 #include<cmath> 7 using namespace std; 8 const int mod=1e8; 9 int read(){ 10 int x=0,f=1;char ch=getchar(); 11 while(ch<'0' || ch>'9'){if(ch=='-')f=-1;ch=getchar();} 12 while(ch>='0' && ch<='9'){x=x*10+ch-'0';ch=getchar();} 13 return x*f; 14 } 15 int m,n; 16 bool mp[13][13]; 17 int f[13][1<<12]; 18 int t[1<<12],cnt=0; 19 int ban[13]; 20 void init(){ 21 int ed=(1<<n)-1; 22 for(int i=0;i<ed;i++){ 23 int j,tmp=1; 24 for(j=0;j<n-1;j++){ 25 if((i&tmp) && (i&(tmp<<1)))break; 26 tmp<<=1; 27 } 28 if(j!=n-1)continue; 29 t[++cnt]=i; 30 } 31 return; 32 } 33 int main(){ 34 int i,j; 35 m=read();n=read(); 36 for(i=1;i<=m;i++){ 37 for(j=0;j<n;j++){ 38 mp[i][j]=read(); 39 if(!mp[i][j])ban[i]|=(1<<j); 40 } 41 } 42 init(); 43 // for(i=1;i<=cnt;i++)printf("%d ",t[i]);printf(" "); 44 f[0][1]=1; 45 int ed=(1<<n)-1; 46 for(i=1;i<=m;i++){ 47 for(int p=1;p<=cnt;p++){ 48 if(t[p]&ban[i-1])continue; 49 for(int r=1;r<=cnt;r++){ 50 if(t[r]&ban[i])continue; 51 if(t[p]&t[r])continue; 52 f[i][r]=(f[i][r]+f[i-1][p])%mod; 53 } 54 } 55 } 56 int ans=0; 57 for(i=1;i<=cnt;i++){ 58 ans=(ans+f[m][i])%mod; 59 } 60 printf("%d ",ans); 61 return 0; 62 }