POJ 3254 Corn Fields(状压DP)
来源:互联网 发布:富士 打印软件 编辑:程序博客网 时间:2024/06/06 19:56
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
USACO 2006 November Gold
dp[i][j]表示遍历到第i行,第i行的状态为j时的方案的数量
状态转移:dp[k][i]=Σdp[k-1][j] ( j为符合条件的所有状态 )
#include <stdio.h>#include <string.h>#include <math.h>#include <algorithm>#define mod 100000000const int inf=1<<30;using namespace std;int dp[20][10000],a[20],b[10000],m,n,cnt;int judge(int x){if(x&(x<<1))return 0;return 1;}int main (){int i,j,k,x,ans=0;scanf("%d %d",&m,&n);for(i=0;i<(1<<n);i++){if(judge(i)){b[++cnt]=i;}}for(i=1;i<=m;i++){for(j=1;j<=n;j++){scanf("%d",&x);if(x==0)a[i]+=(1<<(j-1));}}for(i=1;i<=cnt;i++){if((b[i]&a[1])==0)dp[1][i]=1;}for(k=2;k<=m;k++){for(i=1;i<=cnt;i++){if(a[k]&b[i])continue;for(j=1;j<=cnt;j++){if((a[k-1]&b[j]) || (b[i]&b[j]))continue;dp[k][i]=(dp[k][i]+dp[k-1][j])%mod;}}}for(i=1;i<=cnt;i++)ans=(ans+dp[m][i])%mod;printf("%d\n",ans);return 0;}
- 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)
- [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
- POJ 3254 Corn Fields 【状压DP】
- Corn Fields - POJ 3254 状压dp
- 欢迎使用CSDN-markdown编辑器
- POJ
- 相对布局的layout_alignLeft属性
- js清空input file的值
- 计算机视觉领域的一些好的地址链接
- POJ 3254 Corn Fields(状压DP)
- csrf攻击
- 同时配置redis和hbase的配置文件遇到的问题
- python中zip函数
- python 进程间通信
- 怎么简单的理解对称加密和非对称
- iOS xcode 代码行数统计
- 条件随机场总结
- 安卓直播界面开发,自定义照相机拍照,前后摄像头切换教程加源码!