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.
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)
Line 1: One integer: the number of ways that FJ can choose the squares modulo 100,000,000.
在一个 0,1 组成的数字矩阵中选取 0,1,2..k 个 1 ,且所有的 1 都不相邻的方案总数。
dp[i][j][k] 代表第 i 行的状态为 j (二进制形式),且总共挑选了 k 个 1 满足题意的前 i 行选择方案数
我们确定一个 judge 方法,其目的是判断一个整数 x 的二进制中是否含有相邻的 1 ,若有,返回结果 -1 ,否则返回该二进制数中 1 的个数
于是,对于第一行,所有的合法状态的 dp 取值都为 1
对于第 i 行的某个状态 j ,显然它可以由上一行与当前行所有不相交的方案转移而来
已知第 i-1 行的最大可行方案为 ~j&((1<<m)-1) ,则我们只需要枚举其二进制的子集即可
状态转移方程:
dp[i][j][num+k]+=dp[i-1][s][k]
其中, num 为当前行合法状态中 1 的个数, k 为前 i-1 行挑选的数目, s 为第 i-1 行一个合法的不相交子集。