Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 6197 | Accepted: 3283 |
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
Output
Sample Input
2 3 1 1 1 0 1 0
Sample Output
9
Hint
1 2 3 4
更推荐采用搜索方式,大部分情况下搜索方式效率更高和更容易操作,缺点是内存有时候可能更高点
双循环方式有时候数据大了会超时,另外在可能的情况下也可以采用邻接表方式,将j能到达状态i建立一条边,就不用每次枚举所有状态(搜索方式就是这种思想,通过状态i去寻找可能到达状态i的状态j,而不是所有去枚举)
搜索方式:
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <string> #include <queue> #include <algorithm> #include <map> #include <cmath> #include <iomanip> #define INF 99999999 typedef long long LL; using namespace std; const int MAX=(1<<12)+10; const int mod=100000000; int n,m; int temp[MAX],dp[MAX],s[15][15],bin[15]; bool check(int k,int i){ for(int j=0;j<m;++j){ if(i&1){ if(s[k][j] == 0)return false; i>>=1; if(i&1)return false; }else i>>=1; } return true; } void dfs(int id,int k,int i,int j){ if(k >= m){dp[i]=(dp[i]+temp[j])%mod;return;} dfs(id,k+1,i,j); if(s[id-1][k] == 1 && !((i>>k)&1))dfs(id,k+2,i,j|(1<<k)); } void DP(){ for(int k=1;k<=n;++k){ for(int i=0;i<bin[m];++i)dp[i]=0; for(int i=0;i<bin[m];++i){//这里还可以继续优化,可以先搜索出可能的状态i再来枚举 if(!check(k,i))continue; dfs(k,0,i,0); } for(int i=0;i<bin[m];++i)temp[i]=dp[i]; } } void Init(){ memset(temp,0,sizeof temp); temp[0]=1; } int main(){ bin[0]=1; for(int i=1;i<13;++i)bin[i]=bin[i-1]<<1; while(~scanf("%d%d",&n,&m)){ for(int i=1;i<=n;++i){ for(int j=0;j<m;++j)scanf("%d",&s[i][j]); } Init(); DP(); int sum=0; for(int i=0;i<bin[m];++i)sum=(sum+temp[i])%mod; printf("%d\n",sum); } return 0; }
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <string> #include <queue> #include <algorithm> #include <map> #include <cmath> #include <iomanip> #define INF 99999999 typedef long long LL; using namespace std; const int MAX=377+10; const int mod=100000000; int n,m,nowsize,lastsize; int now[MAX],last[MAX]; int temp[MAX],dp[MAX],s[15][15]; void dfs(int id,int k,int p){ if(k >= m){now[++nowsize]=p;return;} dfs(id,k+1,p); if(s[id][k] == 1)dfs(id,k+2,p|(1<<k)); } void DP(){ lastsize=1; last[1]=0; temp[1]=1; for(int k=1;k<=n;++k){ nowsize=0; dfs(k,0,0); for(int i=1;i<=nowsize;++i)dp[i]=0; for(int i=1;i<=nowsize;++i){ for(int j=1;j<=lastsize;++j){ if(now[i] & last[j])continue; dp[i]=(dp[i]+temp[j])%mod; } } for(int i=1;i<=nowsize;++i)temp[i]=dp[i]; for(int i=1;i<=nowsize;++i)last[i]=now[i]; lastsize=nowsize; } } int main(){ while(~scanf("%d%d",&n,&m)){ for(int i=1;i<=n;++i){ for(int j=0;j<m;++j)scanf("%d",&s[i][j]); } DP(); int sum=0; for(int i=1;i<=lastsize;++i)sum=(sum+temp[i])%mod; printf("%d\n",sum); } return 0; }