POJ P3254 Corn fields 【状压dp】
来源:互联网 发布:javascript 设计模式 编辑:程序博客网 时间:2024/06/05 07:05
Description
Farmer John has purchased a lush new rectangular pasture composed ofM byN (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: Linei+1 describes rowi of the pasture withN 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.
Source
[Submit] [Go Back] [Status] [Discuss]
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#define LL long long int#define REP(i,n) for (int i = 1; i <= (n); i++)#define fo(i,x,y) for (int i = (x); i <= (y); i++)#define Redge(u) for (int k = head[u]; k != -1; k = edge[k].next)using namespace std;const int maxn = 20,maxm = 1 << 12,INF = 1000000000,P = 100000000;inline int read(){int out = 0,flag = 1;char c = getchar();while (c < 48 || c > 57) {if (c == '-') flag = -1; c = getchar();}while (c >= 48 && c <= 57) {out = out * 10 + c - 48; c = getchar();}return out * flag;}int f[maxn][maxm],N,M,le[maxm];bool ill[maxm];int main(){N = read();M = read();int maxv = (1 << M) - 1;for (int i = 1; i <= N; i++){for (int j = 1; j <= M; j++)le[i] = (le[i] << 1) + read();}for (int s = 0; s <= maxv; s++){bool sig = false; int t = s;while (t){if ((t & 1)&& sig) {ill[s] = true; break;}else if (t & 1) sig = true;else sig = false;t >>= 1;}}for (int s = 0; s <= maxv; s++){if (!ill[s] && (s | le[1]) == le[1])f[1][s] = 1;}for (int i = 2; i <= N; i++)for (int e = 0; e <= maxv; e++){if (ill[e] || (e | le[i]) != le[i]) continue;for(int s = 0; s <= maxv; s++){if (e & s) continue;f[i][e] = (f[i][e] + f[i - 1][s]) % P;}}int ans = 0;for (int i = 0; i <= maxv; i++) ans = (ans + f[N][i]) % P;cout<<ans<<endl;return 0;}
- POJ P3254 Corn fields 【状压dp】
- POJ 3254 Corn Fields(状压dp)
- [poj 3254]Corn Fields[状压DP]
- poj 3254 Corn Fields 状压dp
- POJ 3254 Corn Fields 【状压DP】
- Corn Fields - POJ 3254 状压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 (状压DP)
- poj 3254 Corn Fields 状压dp入门
- poj 3254 Corn Fields 【状压 DP 入门】
- 【状压dp】poj 3254 Corn Fields
- poj 3254 Corn Fields (状压DP)
- POJ 3254 Corn Fields (状压dp)
- 【POJ 3254】 Corn Fields(状压DP)
- 面向对象程序设计上机练习六(类和对象)
- 文章标题
- 油田采油生产业务建模之业务用例实践(EA使用入门)
- 拷贝构造函数、析构函数
- Python-数据分析:Numpy库介绍
- POJ P3254 Corn fields 【状压dp】
- H5开发第三方选择
- Ubuntu安装软件及遇到问题解决
- hadoop文档
- Scrapy--爬取全国天气信息
- Linux实验四
- JDK9.0.1的安装与配置
- java运算
- 念整数 乐学题目10.29完成