poj3254 Corn Fields----dp状态压缩
来源:互联网 发布:淘宝发货时间规定 编辑:程序博客网 时间:2024/06/05 18: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.
Source
#include<iostream>#include<cstdlib>#include<stdio.h>#include<memory.h>#define ll long long#define maxn (1<<13)#define mod 100000000using namespace std;int n,m;int dp[15][maxn];//第i行状态为j时满足的方案数。bool flag[15][maxn];//判断第i行状态为j时是否合法。int mat[15];int mm;void init(){ for(int i=1;i<=n;i++) for(int j=0;j<=mm;j++) { flag[i][j]=true; //原来第i行的状态取反与j状态相与为1说明j状态在没草的地方放牛,所以不合法即~mat[i]&j //判断j状态是否有相邻的两个1,若有即不合法,用j&(j<<1)判断,j<<1即j右移1位 //eg:01101--->11010,相与为1,即可判断出有相邻的1,不合法。 if((~mat[i]&j)||(j&(j<<1))) flag[i][j]=false; }}int main(){ while(scanf("%d%d",&n,&m)!=EOF) { memset(dp,0,sizeof(dp)); memset(mat,0,sizeof(mat)); int num; for(int i=1;i<=n;i++) for(int j=m-1;j>=0;j--) { scanf("%d",&num); if(num==1)mat[i]|=(1<<j); } mm=(1<<m)-1; init(); for(int i=0;i<=mm;i++) if(flag[n][i]) dp[n][i]=1;//初始化为1 int i,j; for(i=n-1;i>=1;i--)//自底向上 { for(j=0;j<=mm;j++) { if(!flag[i][j]) continue; for(int k=0;k<=mm;k++)//枚举i+1行的状态 { if(!flag[i+1][k]) continue; if(!(j&k))//i+1行的状态与i行的状态不冲突 dp[i][j]+=dp[i+1][k]; } dp[i][j]%=mod; } } int ans=0; for(i=0;i<=mm;i++) ans+=dp[1][i]%mod; ans%=mod; cout<<ans<<endl; }}
- 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正则表达式详解
- 报告称公司高管年薪5年涨37万 员工10年涨3万-高管-员工-年薪
- 百万级访问网站前期的技术准备(下)
- SolarWinds网络工程师工具集
- Debian下设置网络
- poj3254 Corn Fields----dp状态压缩
- CSS兼容IE/Firefox要点
- 英语词汇记忆!
- sysctl命令
- Android4.0 EXIF解析
- 使用Javascript和prototype.js框架创建类型及其相关的prototype属性的简单介绍
- 区分Activity的四种加载模式----以及Intent的setFlags
- Activemq+spring的第一个程序(入门程序--内嵌Broker--消息队列)
- 裸设备绑定出现“Cannot open master raw device '/dev/rawctl' (No such file or directory)”