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.
给出一个n行m列的草地,1表示肥沃,0表示贫瘠,现在要把一些牛放在肥沃的草地上,但是要求所有牛不能相邻,问你有多少种放法
题目分析
状压DP入门题 用来练手
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <algorithm>
using namespace std;
int n,m;
bool book[1100][1<<13];
int map[20][1<<13];
int dp[20][1<<13];
int mod=1000000000;
int ans;
void Dp()
{
for(int i=1;i<=map[1][0];i++)
dp[1][map[1][i]]=1;
for(int i=2;i<=n;i++)
for(int j=1;j<=map[i][0];j++)
for(int k=1;k<=map[i-1][0];k++)
if(book[map[i][j]][map[i-1][k]])
dp[i][map[i][j]]=(dp[i][map[i][j]]+dp[i-1][map[i-1][k]])%mod;
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
int tmp;
int s=0;
for(int j=1;j<=m;j++)
{
scanf("%d",&tmp);
if(tmp==0) s+=(1<<(j-1));
}
for(int j=0;j<(1<<m);j++)
{
if((j&s)||(j&(j<<1)))
continue;
map[i][++map[i][0]]=j;
}
}
for(int i=0;i<(1<<m);i++)
for(int j=0;j<(1<<m);j++)
if(!(i&j))book[i][j]=true;
Dp();
for(int i=0;i<(1<<m);i++)
{
ans+=dp[n][i];
ans=ans%mod;
}
printf("%d",ans);
return 0;
}