POJ 3254 Corn Fields (状压dp)
来源:互联网 发布:mac安装win10激活工具 编辑:程序博客网 时间:2024/05/16 15:26
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.
Source
#include <cstdio>int const MOD = 1e8;int n, m, a[20][20];int dp[16][1 << 16];int sta[1 << 16];int main(){scanf("%d %d", &n, &m);for(int i = 0; i < n; i++){for(int j = 0; j < m; j++){scanf("%d", &a[i][j]);if(a[i][j])sta[i] |= (1 << j);}}for(int i = 0; i < (1 << m); i++)if((i | sta[0]) == sta[0] && !(i & (i >> 1)))dp[0][i] = 1;for(int i = 1; i < n; i++)for(int j = 0; j < (1 << m); j++)if((j | sta[i]) == sta[i] && !(j & (j >> 1)))for(int k = 0; k < (1 << m); k++)if(dp[i - 1][k] && !(k & j))dp[i][j] = (dp[i][j] + dp[i - 1][k]) % MOD;int ans = 0;for(int i = 0; i < (1 << m); i++)ans = (ans + dp[n - 1][i]) % MOD;printf("%d\n", ans);}
- 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)
- POJ 3254 Corn Fields (状压DP)
- Activity,Fragment数据回传
- 4-3-串的块链存储结构-串-第4章-《数据结构》课本源码-严蔚敏吴伟民版
- 06-TOM汤姆猫
- Linux系统操作mysql数据库
- Typedef 学习
- POJ 3254 Corn Fields (状压dp)
- bug1:unrecognized selector sent to instance
- [Django]SE项目回忆录(二)-注册/登录功能的实现及细节
- 【Akka】Akka入门编程实例
- 【Cygwin】安装后修改默认用户名
- [7]姥爷幽默谈Objective-C-属性@property,类别Category
- PAT 1001
- Could not generate CGLIB subclass of class编译时出错
- vsrs3.5 opencv2.4.9 vs13 配置