Corn Fields

Time Limit: 2000MS Memory Limit: 65536K

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.

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.

思路:

在状态压缩方程里面,最难的应该就是位运算以及状态方程了,本题的话,用book,位运算存一排的0和1,然后用mp计算出一排里面两两不相邻的情况,状态就那样,所以不相邻的情况是固定了,就那么多。然后就是循环了,第一个是行,第二个是所在行,第三个是上一行,中间的判定条件,第一个是判断这种状态是否在样例行的子集,是否出出现两两不相邻的情况,第二个是判断是否两行出现上下相邻。

#include <iostream>
using namespace std;
const int maxn = 12;
const int mod = 1e+9; 
int dp[maxn + 5][1 << maxn + 5], a[maxn + 5][maxn + 5], book[1 << maxn + 5];
bool mp[1 << maxn + 5];
int main() {
	int n, m;
	scanf("%d %d", &n, &m);
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			scanf("%d", &a[i][j]);
			book[i] = (book[i] << 1) + a[i][j];
		}
	}
	for (int i = 0; i < 1 << m; i++) mp[i] = ((i & (i << 1)) == 0) && ((i & (i >> 1)) == 0);
	dp[0][0] = 1;
	for (int i = 1; i <= n; i++) {
		for (int s = 0; s < 1 << m; s++) {
			if ((book[i] & s) == s && mp[s]) {
				for (int j = 0; j < 1 << m; j++) {
					if (!(j & s)) dp[i][s] = (dp[i][s] + dp[i - 1][j]) % mod;
				}
			}
		}
	}
	int ans = 0;
	for (int i = 0; i < 1 << m; i++) ans = (ans + dp[n][i]) % mod;
	cout << ans << endl;
	return 0;
}