Corn Fields(poj3254)状态压缩dp
来源:互联网 发布:室内装修平面设计软件 编辑:程序博客网 时间:2024/06/09 19:57
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<iostream>#include<cstdio>#include<cstring>using namespace std;const int N=13;const int M=1<<13;const int Mod=1e8;int dp[N][M];//dp[i][j]表示第i行状态为j的方法数int Map[N];//每一行给出地的状态int state[M];//状态bool judge(int i,int x)//第i行是否可以放状态x,false可以,true不可以{ return (Map[i]&state[x]);}int main(){ memset(dp,0,sizeof(dp)); memset(state,0,sizeof(state)); memset(Map,0,sizeof(Map)); int m,n,x; //m行n列 scanf("%d%d",&m,&n); for(int i=1;i<=m;i++){ for(int j=1;j<=n;j++){ scanf("%d",&x); if(x==0){ Map[i]+=1<<(j-1); } } } int k=0; for(int i=0;i<(1<<n);i++){ if(!(i&(i<<1))){ state[k++]=i; } } //总共有k种状态满足条件:不存在相邻的1 for(int i=0;i<k;i++){ if(!judge(1,i)){ dp[1][i]=1; } } for(int i=2;i<=m;i++){ for(int j=0;j<k;j++){ if(judge(i,j)) continue;//第i行不能按状态j放牛 for(int f=0;f<k;f++){ if(judge(i-1,f)) continue;//第i-1行不能按状态f放牛 if(!(state[j]&state[f])){//判断第i行与第i-1行是否存在相邻的1 dp[i][j]+=dp[i-1][f]; } } } } int ans=0; for(int i=0;i<k;i++){ ans+=dp[m][i]; ans%=Mod; } cout<<ans<<endl; return 0;}
- poj3254 Corn Fields----dp状态压缩
- poj3254 - Corn Fields(状态压缩dp)
- poj3254 Corn Fields (状态压缩dp)
- POJ3254 Corn Fields 状态压缩DP
- POJ3254 Corn Fields 状态压缩DP
- POJ3254 Corn Fields (状态压缩DP)
- 状态压缩DP解POJ3254-Corn Fields
- Corn Fields(poj3254)状态压缩dp
- poj3254 状态压缩dp-Corn Fields
- 【POJ3254】Corn Fields(状态压缩DP)
- 【poj3254】Corn Fields 状态压缩dp
- 初识状态压缩dp poj3254 Corn Fields
- poj3254 Corn Fields 状态压缩
- POJ3254:Corn Fields(状态压缩)
- poj3254 Corn Fields (状态压缩)
- [状态压缩]poj3254 Corn Fields
- POJ3254-Corn Fields-状态压缩
- POJ3254 Corn Fields(状态DP)
- Java 异常
- 【NSBD】——My first Map
- Oracle 函数
- 数据结构实验之二叉树的建立与遍历
- 实现一个函数,把字符数组中的每个空格替换成“%20”。 例如输入“we are happy.”,则输出“we%20are%20happy”
- Corn Fields(poj3254)状态压缩dp
- Cookie到底是干什么的?
- maven 部署到tomcat6/tomcat7
- [Android] Qt安卓教程(1): 从Qt5.1开始
- 风雨见彩虹
- [Go语言]我的第五个Go语言程序
- Max Points on a Line
- SQLite数据库
- poj2377Bad Cowtractors