POJ - 3254 Corn Fields (状态压缩 + DFS)

来源:互联网 发布:奥威软件 编辑:程序博客网 时间:2024/06/10 12:14
Corn Fields
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 13395 Accepted: 7032

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 31 1 10 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块地,其中一些地荒掉了。玉米是一种傲娇的植物,种在相邻的地里会导致不孕不育。求所有种法数对100000000求余。
解题思路:对每一行用状态压缩来表示,然后用DFS来判断是否在此处种植即可
其中技巧:
对于相邻的土地不能种植以及荒废的土地不能种植的快速判断:将荒废土地的二进制取反保存,然后将上一层的土地与本层荒废土地进行或,其中为1的表示不能够种植的,为0表示可以进行种植
/*头文件模板*/#include <map>#include <set>#include <cmath>#include <ctime>#include <queue>#include <stack>#include <vector>#include <cctype>#include <cstdio>#include <string>#include <cstring>#include <sstream>#include <cstdlib>#include <iomanip>#include <typeinfo>#include <iostream>#include <algorithm>#include <functional>using namespace std;#define pb push_back#define mp make_pair#define mem(a, x) memset(a, x, sizeof(a))#define copy(a, b) memcpy(a, b, sizeof(a))#define lson rt << 1, l, mid#define rson rt << 1|1, mid + 1, r#define FIN freopen("input.txt", "r", stdin)#define FOUT freopen("output.txt", "w", stdout)typedef long long LL;typedef pair<int, int > PII;typedef pair<int, string> PIS;typedef pair<LL, LL>PLL;typedef unsigned long long uLL;template<typename T>void print (T* p, T* q, string Gap = " ", bool flag = false) {int d = p < q ? 1 : -1;while (p != q) {if (flag) cout << Gap[0] << *p << Gap[1];else cout << *p;p += d;if (p != q && !flag) cout << Gap;}cout << endl;}template<typename T>void print (const T &a, string bes = "") {int len = bes.length();if (len >= 2) cout << bes[0] << a << bes[1] << endl;else cout << a << endl;}template<typename T>void debug (T* p, T* q, string Gap = " ", bool flag = false) {#ifndef ONLINE_JUDGEint d = p < q ? 1 : -1;cout << "Debug out : ";while (p != q) {if (flag) cout << Gap[0] << *p << Gap[1];else cout << *p;p += d;if (p != q && !flag) cout << Gap;}cout << endl;#endif}template<typename T>void debug (const T &a, string bes = "") {#ifndef ONLINE_JUDGEint len = bes.length();cout << "Debug out : ";if (len >= 2) cout << bes[0] << a << bes[1] << endl;else cout << a << endl;#endif}void IO_Init() {ios::sync_with_stdio (false);}LL LLabs (const LL a) {return a >= 0 ? a : -a;}const double PI = 3.1415926535898;const double eps = 1e-10;const int MAXM = 1e5 + 5;const int MAXN = 1e5 + 5;const int INF = 0x3f3f3f3f;const int mod = 1e8;/*头文件模板*/LL dp[15][1 << 15];int F[15];int M, N;void dfs(int r, int l, int k,int kc, int prev) {if(l == N) {dp[r][k] = (dp[r][k] + prev) % mod;return;}dfs(r, l + 1, k, kc, prev);if(~ kc >> l & 1) {dfs(r, l + 1, k | 1 << l, kc | 1 << l + 1, prev);}}int main() {#ifndef ONLINE_JUDGE//FIN;//FOUT;#endifIO_Init();while(~scanf("%d%d", &M, &N)) {mem(dp, 0);int x;for(int i = 1; i <= M; i ++) {for(int j = 0; j < N; j ++) {scanf("%d", &x);F[i] |= !x << j;}}dp[0][0] = 1;for(int i = 1; i <= M; i ++) {for(int j = 0; j < 1 << N; j ++) {dfs(i, 0, 0, F[i] | j, dp[i - 1][j]);}}LL ans = 0;for(int i = 0; i < 1 << N; i ++) {ans = (ans % mod + dp[M][i] % mod) % mod;}printf("%d\n", ans);}return 0;}


1 0
原创粉丝点击