POJ - 3254 Corn Fields (状态压缩 + DFS)
来源:互联网 发布:奥威软件 编辑:程序博客网 时间:2024/06/10 12:14
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
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
Sample Input
2 31 1 10 1 0
Sample Output
9
Hint
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.
/*头文件模板*/#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;}
- POJ - 3254 Corn Fields (状态压缩 + DFS)
- poj 3254 Corn Fields //状态压缩DP
- POJ 3254 Corn Fields 状态压缩DP
- poj 3254 Corn Fields(状态压缩dp)
- POJ 3254 Corn Fields (状态压缩DP)
- poj-3254-Corn Fields-状态压缩DP
- poj 3254 Corn Fields (状态压缩DP)
- Poj 3254 Corn Fields 状态压缩
- POJ 3254 Corn Fields (状态压缩DP)
- POJ 3254 Corn Fields(状态压缩)
- POJ 3254 Corn Fields(状态压缩dp)
- POJ 3254 Corn Fields 状态压缩dp
- POJ 3254 Corn Fields 状态压缩DP
- poj 3254 Corn Fields ,状态压缩DP
- poj Corn Fields 3254 状态压缩dp
- POJ 3254 Corn Fields 状态压缩
- poj 3254 Corn Fields (状态压缩DP)
- POJ 3254 Corn Fields(状态压缩)
- C++ 虚继承
- CSU 1779 NBUT 1641 “错误的算法”(这是题目名)
- 深入理解HTTP协议
- React Native商城项目实战04 - 封装TabNavigator.Item的创建
- mysql中的锁
- POJ - 3254 Corn Fields (状态压缩 + DFS)
- JDBC连接数据库
- Kotlin vs Java on Android
- 【WEB-INF】WEB-INF是Java的WEB应用的安全目录
- HDU-1159-CommonSubsequence(LCS最长公共子序列)
- spring框架学习(一)
- Ubuntu安装supervisorctl
- grep 和find 命令
- https原理及tomcat配置https方法