1. 程式人生 > >[USACO06NOV]玉米田Corn Fields 狀壓dp BZOJ1725

[USACO06NOV]玉米田Corn Fields 狀壓dp BZOJ1725

題目描述

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.

農場主John新買了一塊長方形的新牧場,這塊牧場被劃分成M行N列(1 ≤ M ≤ 12; 1 ≤ N ≤ 12),每一格都是一塊正方形的土地。John打算在牧場上的某幾格裡種上美味的草,供他的奶牛們享用。

遺憾的是,有些土地相當貧瘠,不能用來種草。並且,奶牛們喜歡獨佔一塊草地的感覺,於是John不會選擇兩塊相鄰的土地,也就是說,沒有哪兩塊草地有公共邊。

John想知道,如果不考慮草地的總塊數,那麼,一共有多少種種植方案可供他選擇?(當然,把新牧場完全荒廢也是一種方案)

輸入輸出格式

輸入格式:

 

第一行:兩個整數M和N,用空格隔開。

第2到第M+1行:每行包含N個用空格隔開的整數,描述了每塊土地的狀態。第i+1行描述了第i行的土地,所有整數均為0或1,是1的話,表示這塊土地足夠肥沃,0則表示這塊土地不適合種草。

 

輸出格式:

 

一個整數,即牧場分配總方案數除以100,000,000的餘數。

 

輸入輸出樣例

輸入樣例#1: 複製

2 3
1 1 1
0 1 0

輸出樣例#1: 複製

9

 


用二進位制數代表01狀態;

用 judge 表示該狀態的合法性;

然後列舉每一行的狀態;

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
//#include<cctype>
//#pragma GCC optimize("O3")
using namespace std;
#define maxn 100005
#define inf 0x3f3f3f3f
#define INF 999999999
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
#define rdult(x) scanf("%lu",&x)
#define rdlf(x) scanf("%lf",&x)
#define rdstr(x) scanf("%s",x)
typedef long long  ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const long long int mod = 1e9 + 7;
#define Mod 1000000000
#define sq(x) (x)*(x)
#define eps 1e-3
typedef pair<int, int> pii;
#define pi acos(-1.0)
const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++)
inline ll rd() {
	ll x = 0;
	char c = getchar();
	bool f = false;
	while (!isdigit(c)) {
		if (c == '-') f = true;
		c = getchar();
	}
	while (isdigit(c)) {
		x = (x << 1) + (x << 3) + (c ^ 48);
		c = getchar();
	}
	return f ? -x : x;
}

ll gcd(ll a, ll b) {
	return b == 0 ? a : gcd(b, a%b);
}
ll sqr(ll x) { return x * x; }

/*ll ans;
ll exgcd(ll a, ll b, ll &x, ll &y) {
	if (!b) {
		x = 1; y = 0; return a;
	}
	ans = exgcd(b, a%b, x, y);
	ll t = x; x = y; y = t - a / b * y;
	return ans;
}
*/

int n, m;
int G[20][20];
int judge[1 << 12 + 10];
int dp[20][1 << 12 + 10];
int f[1 << 12 + 10];

int main()
{
	//ios::sync_with_stdio(false);
	rdint(m); rdint(n);
	for(int i=1;i<=m;i++)
		for (int j = 1; j <= n; j++) {
			rdint(G[i][j]); f[i] = (f[i] << 1) + G[i][j];
		}
	for (int i = 0; i < (1 << n); i++)
		judge[i] = (((i&(i << 1)) == 0) && ((i&(i >> 1)) == 0));// 預處理合法狀態
	
	dp[0][0] = 1;
	for (int i = 1; i <= m; i++) {
		// 列舉每一行
		for (int j = 0; j < (1 << n); j++) {
			// 列舉狀態
			if (judge[j] && (j&f[i]) == j) {
				for (int k = 0; k < (1 << n); k++) {
					// 列舉上一行狀態
					if ((k&j) == 0) {
						dp[i][j] = (dp[i][j] + dp[i - 1][k]) % Mod;
					}
				}
			}
		}
	}
	int ans = 0;
	for (int i = 0; i < (1 << n); i++) {
		ans = (ans + dp[m][i]) % Mod;
	}
	printf("%d\n", ans);
    return 0;
}