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
Hint
Number the squares as follows:
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.
相较于炮兵阵地还是没那么六 毕竟简单题
同样 枚举状态放在s数组里面
还有要说的都在注释里面了
#include<cstdio>
#include<algorithm>
#include<cstring>
#define mod 1000000000
#define clr(a,b) memset(a,b,sizeof(a));
#include<iostream>
using namespace std;
int maxn=0;
int dp[20][1000];
int cur[20];
int land[20];
int s[1000];
int m,n;
void init()
{
maxn=0;
for(int i=0;i<(1<<n);i++)
{
if((i&(i<<1))==0) s[++maxn]=i;
}
}
int main()
{
while(scanf("%d%d",&m,&n)!=EOF)
{
init();
//printf("%d\n",maxn);
clr(land,0)
clr(dp,0)
int temp=0;
//printf("stop");
for(int i=1;i<=m;i++)
{
//printf("stop");
for(int j=1;j<=n;j++)
{
scanf("%d",&temp);
if(temp==0) land[i]=land[i]|(1<<(n-j));//不能放的存为1 这样用land[i] 和状态s按位与一下 出现1就可以表示不合法辣
}
}
//cout<<land[1]<<land[2]<<endl;
for(int i=1;i<=maxn;i++)
{
if((land[1]&s[i])==0)
dp[1][i]=1;
}
for(int i=2;i<=m;i++)
{
for(int j=1;j<=maxn;j++)
{
if((land[i]&s[j])==0) //s[j]合法
for(int k=1;k<=maxn;k++)//枚举上一层状态
{
if((land[i-1]&s[k])==0&&(s[k]&s[j])==0) //s[k]合法
dp[i][j]=(dp[i][j]+dp[i-1][k])%mod;//求方法总数 所以相加
}
}
}
int ans=0;
for(int i=1;i<=maxn;i++)
{
ans=(ans+dp[m][i])%mod;
}
printf("%d\n",ans);
}
}