poj_3254 Corn Fields(状压dp)
来源:互联网 发布:centos防火墙关闭端口 编辑:程序博客网 时间:2024/06/11 20:14
Description
Farmer John has purchased a lush new rectangular pasture composed of M 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: Line i+1 describes row i 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.
#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>#include <stack>#include <bitset>#include <queue>#include <set>#include <map>#include <string>#include <algorithm>#define FOP freopen("data.txt","r",stdin)#define FOP2 freopen("data1.txt","w",stdout)#define inf 0x3f3f3f3f#define maxn 1000010#define mod 100000000#define PI acos(-1.0)#define LL long longusing namespace std;int n, m;int dp[20][1<<12];int ma[20];int main(){ //FOP; while(~scanf("%d%d", &n, &m)) { memset(ma, 0, sizeof(ma)); memset(dp, 0, sizeof(dp)); int f; for(int i = 1; i <= n; i++) { for(int j = 0; j < m; j++) { scanf("%d", &f); if(f) ma[i] |= 1<<j; } } for(int j = 0; j < 1<<m; j++) { if((j&(j<<1)) || ((j&ma[1]) != j && j)) continue; dp[1][j] = 1; } for(int i = 2; i <= n; i++) { for(int j = 0; j < 1<<m; j++) { if((j&(j<<1)) || ((j&ma[i]) != j && j)) continue; for(int k = 0; k < 1<<m; k++) { if(k&(k<<1) || (j&k)) continue; dp[i][j] = dp[i][j] + dp[i-1][k]; } } } int ans = 0; for(int i = 0; i < 1<<m; i++) ans = (ans + dp[n][i])%mod; printf("%d\n", ans); } return 0;}
- poj_3254 Corn Fields(状压dp)
- Corn Fields----状压DP
- Corn Fields 状压DP
- 状压dp Corn Fields
- POJ 3254 Corn Fields(状压dp)
- poj3254(Corn Fields)状压dp
- POJ 3254 Corn Fields(状压dp)
- POJ 3254-Corn Fields(状压DP)
- poj3254--Corn Fields(状压dp)
- POJ 3254 Corn Fields (状压DP)
- 【POJ 3254】 Corn Fields(状压DP)
- POJ 3254 Corn Fields (状压DP)
- poj3254 Corn Fields(状压dp)
- 【POJ 3254】Corn Fields(状压dp)
- POJ 3254 Corn Fields (状压DP)
- POJ3254-Corn Fields(状压DP)
- POJ3254:Corn Fields(状压dp入门)
- [POJ 3254]Corn Fields(状压DP)
- c# 解决 EntityFramework 基础提供程序在 Open 上失败
- Activity启动流程笔记(一)
- 初识Java虚拟机(10)动态分派的实现
- 数字基带传输与码间干扰
- 共同学习Java源代码-数据结构-TreeSet(二)
- poj_3254 Corn Fields(状压dp)
- 设计模式总论
- Qt一些宏的用法
- 关于Windows系统中一些实用的修改常识
- 线性规划与网络流24——航空路线问题
- 逆序对-洛谷 1908
- EXC_BAD_ACCESS(内存过度释放问题)
- dom解析 jdk自己提供的
- 图割论文阅读笔记:Implementing GrabCut