Corn Fields

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.


题意:

给n*m的大小的玉米地,1表示这的土地肥沃,反之表示这土地贫瘠,奶牛在肥沃的土地,且奶牛互不相邻,求分配的方案(不进行分配也是一种方案)


状态压缩+dp

每一行都有2的m次方个状态,e.g m=3 有000----111 可是这些状态有相邻的,先预处理找到所有不相邻的状态若i&(i<<1)==0,即i就是不相邻的状态,不是所有的行的不相邻的状态都是符合题意的,还与该土地的性质有关,将每一行土地的0,1转化成十进制保存起来它的状态,用数组记录十进制的反,row[i]=(row[i]<<1)|!t,接下来便开始用dp来求解,dp[i][j] i表示第i层,j表示第i层的第j的状态,状态压缩公式:dp[i][j]=dp[i][j]+dp[i-1][k];首先先把第一层的各个符合题意的状态dp[0][j]设置为1,接下来是二层,找出第i-1层符合的题意的状态与第i层符合题意的状态比较,看是否冲突,若不冲突,则dp[i][j]=dp[i][j]+dp[i-1][k],以此类推,求出最后一层,然后将最后一层的方案数求和

代码如下:

#include
#include
using namespace std;
const int N=13;
const int M=378;
const int mod=100000000;
int rec[M],row[N],dp[N][M];
int main()
{
    int n,m;
    int x=1<<12,k=0,t;
    for(int i=0;i




你可能感兴趣的:(dp)