标签:des style blog color io os ar strong for
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 8041 | Accepted: 4287 |
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
思路:首先的得到相邻格子不能同时取的信息,然后,对于不能种的地也要舍去,最后求得不和上一行冲突的状态数总和。
#include"stdio.h" #include"string.h" #include"iostream" #include"algorithm" #include"math.h" #include"vector" using namespace std; #define LL __int64 #define N 13 const int mod=100000000; const int M=1<<12; int g[N][N]; int num[M],a[N][M]; int dp[N][M]; //存储每个格子对应的取法总数 int n,m,lim; //每一行的和就是以该行为土地边界的答案 void inti() //初始化数组,相邻两个格子不能同时取 { int i,k; lim=1<<12; for(i=k=0;i<lim;i++) { if(i&(i<<1)) continue; num[k++]=i; } } int gettmp(int i) //得到每一行的信息,不能取的格子对应位的值置为一 { int tmp=0; for(int j=0;j<m;j++) { tmp<<=1; tmp+=(g[i][j]^1); } return tmp; } void work() { int i,j,k,tmp; lim=1<<m; for(i=1;i<n;i++) { for(j=0;num[j]<lim;j++) a[i][j]=num[j]; tmp=gettmp(i); for(j=0;num[j]<lim;j++) { if(a[i][j]&tmp) //为真表示该种取法包含不能取得格子 { a[i][j]=0; continue; } for(k=0;num[k]<lim;k++) //求该种取法不和上一行冲突的种类数 { if(a[i][j]&a[i-1][k]) continue; dp[i][j]+=dp[i-1][k]; dp[i][j]%=mod; } // printf("%d\n",dp[i][j]); } } int ans=0; for(i=0;num[i]<lim;i++) ans=(ans+dp[n-1][i])%mod; printf("%d\n",ans); } int main() { int i,j; inti(); while(scanf("%d%d",&n,&m)!=-1) { lim=1<<m; memset(dp,0,sizeof(dp)); for(i=0;i<n;i++) for(j=0;j<m;j++) scanf("%d",&g[i][j]); int tmp=gettmp(0); for(i=0;num[i]<lim;i++) { a[0][i]=num[i]; if(num[i]&tmp) { dp[0][i]=0; a[0][i]=0; } else dp[0][i]=1; } work(); } return 0; }
标签:des style blog color io os ar strong for
原文地址:http://blog.csdn.net/u011721440/article/details/39672199