poj 3254Corn Fields (入门状压dp)

时间:2022-08-17 20:04:46
Farmer John has purchased a lush new rectangular pasture composed of M by N ( ≤ M ≤ ;  ≤ N ≤ ) 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 : Two space-separated integers: M and N
Lines .. M+: Line i+ describes row i of the pasture with N space-separated integers indicating whether a square is fertile ( for fertile, for infertile)
Output
Line : One integer: the number of ways that FJ can choose the squares modulo ,,.
Sample Input Sample Output Hint
Number the squares as follows: There are four ways to plant only on one squares (, , , or ), three ways to plant on two squares (, , or ), way to plant on three squares (), and one way to plant on no squares. +++=.

被hdu 多校第3场c题教育了,来刷状压dp

先处理下所有的满足的状态;i&i<<1

然后和上一个状态比较。满足的状态里选出和上一行满足的状态

#include <cstdio>
#include <cstring>
#include <iostream>
//#include <algorithm>
#include <vector>
using namespace std;
#define ll long long
//#define mod 998244353
const int mod=;
int low[];
int temp;
int state[<<];
int dp[][<<];
int main()
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=;i<n;i++)
{
for(int j=;j<m;j++)
{
int num;
scanf("%d",&num);
low[i]=low[i]|(num<<j);
}
}//处理每一行的状态
temp=;
for(int i=;i<(<<m);i++)
{
if((i&(i<<))==)
{
state[temp++]=i;
}
}//处理所有满足的状态
memset(dp,,sizeof dp);
for(int i=;i<temp;i++)
{
if((low[]&state[i])==state[i])
dp[][i]=; }//处理第一行 for(int i=;i<n;i++)
for(int j=;j<temp;j++)
{
if((low[i]&state[j])==state[j])
{
for(int k=;k<temp;k++)
{
if((state[j]&state[k])==)
dp[i][j]=(dp[i][j]+dp[i-][k])%mod;
}
}
}
int ans=;
for(int i=;i<temp;i++)
{
ans=ans+dp[n-][i];
ans=ans%mod;
}
cout<<ans<<endl; return ;
}