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
题目大意:
农夫有一块地,被划分为m行n列大小相等的格子,其中一些格子是可以放牧的(用1标记),农夫可以在这些格子里放牛,其他格子则不能放牛(用0标记),并且要求不可以使相邻格子都有牛。现在输入数据给出这块地的大小及可否放牧的情况,求该农夫有多少种放牧方案可以选择(注意:任何格子都不放也是一种选择,不要忘记考虑!
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const int mod=100000000;
int a[1000],cur[20],num[200];
int n,m,k,indx;
int dp[15][1000];
bool check(int x){
if(x&(x<<1)){//相邻全1不行
return false;
}else{
return true;
}
}
void init(){
//初始化所有m的可能性
indx=0;
int ans=1<<m;//最大+1的可能性
for(int i=0;i<ans;i++){
if(check(i)){//判断i是否可行
a[++indx]=i;
}
}
}
bool checkh(int x,int y){//x能不能在y行出现
if(x&cur[y]){
return false;
}else{
return true;
}
}
int main(){
while(cin>>n>>m){
init();//a[]存储可行方案
//indx 是方案总数
memset(dp,0,sizeof dp);
for(int i=1;i<=n;i++){
cur[i]=0;
int num;
for(int j=1;j<=m;j++){
cin>>num;
if(num==0){
cur[i]+=(1<<(m-j));//第几位可以取1
}
}
}
for(int i=1;i<=indx;i++){//第一行的可行方案
if(checkh(a[i],1)){
dp[1][i]=1;
}
}
for(int i=2;i<=n;i++){
for(int j=1;j<=indx;j++){
if(!checkh(a[j],i))continue;
//a[j]是否可以在i行
for(int z=1;z<=indx;z++){
if(!checkh(a[z],i-1))continue;
//a[z]是否可以在i-1行
if(a[j]&a[z])continue;
//a[j] a[z] 是否有交接点
dp[i][j]+=dp[i-1][z];
dp[i][j]%=mod;
}
}
}
int sum1=0;
for(int i=1;i<=indx;i++){
sum1+=dp[n][i];
sum1%=mod;
}
cout<<sum1<<endl;
}
return 0;
}