poj 3254Corn Fields (入门状压dp)
Farmer John has purchased a lush new rectangular pasture composed of M by N ( ≤ M ≤ ; ≤ N ≤ ) 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
Line : Two space-separated integers: M and N
Lines .. M+: Line i+ describes row i of the pasture with N space-separated integers indicating whether a square is fertile ( for fertile, for infertile)
Output
Line : One integer: the number of ways that FJ can choose the squares modulo ,,.
Sample Input Sample Output Hint
Number the squares as follows: There are four ways to plant only on one squares (, , , or ), three ways to plant on two squares (, , or ), way to plant on three squares (), and one way to plant on no squares. +++=.
被hdu 多校第3场c题教育了,来刷状压dp
先处理下所有的满足的状态;i&i<<1
然后和上一个状态比较。满足的状态里选出和上一行满足的状态
#include <cstdio>
#include <cstring>
#include <iostream>
//#include <algorithm>
#include <vector>
using namespace std;
#define ll long long
//#define mod 998244353
const int mod=;
int low[];
int temp;
int state[<<];
int dp[][<<];
int main()
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=;i<n;i++)
{
for(int j=;j<m;j++)
{
int num;
scanf("%d",&num);
low[i]=low[i]|(num<<j);
}
}//处理每一行的状态
temp=;
for(int i=;i<(<<m);i++)
{
if((i&(i<<))==)
{
state[temp++]=i;
}
}//处理所有满足的状态
memset(dp,,sizeof dp);
for(int i=;i<temp;i++)
{
if((low[]&state[i])==state[i])
dp[][i]=; }//处理第一行 for(int i=;i<n;i++)
for(int j=;j<temp;j++)
{
if((low[i]&state[j])==state[j])
{
for(int k=;k<temp;k++)
{
if((state[j]&state[k])==)
dp[i][j]=(dp[i][j]+dp[i-][k])%mod;
}
}
}
int ans=;
for(int i=;i<temp;i++)
{
ans=ans+dp[n-][i];
ans=ans%mod;
}
cout<<ans<<endl; return ;
}
poj 3254Corn Fields (入门状压dp)的更多相关文章
- poj 3254 Corn Fields (状压dp)(棋盘dp)
状压dp入门题 因为当前行的状态只和上一行有关 所以可以一行一行来做 因为m <= 12所以可以用二进制来表示放了或者没有放 0表示没放,1表示放 f[i][state]表示第i行状态为stat ...
- 动态规划晋级——POJ 3254 Corn Fields【状压DP】
转载请注明出处:http://blog.csdn.net/a1dark 分析:刚开始学状压DP比较困难.多看看就发现其实也没有想象中那么难.这道题由于列数较小.所以将行压缩成二进制来看.首先处理第一行 ...
- POJ - 3254 Corn Fields(状压DP)题解
思路: 参照blog,用状压DP做,和题解稍微有点不一样,我这里直接储存了状态而不是索引. 这一题的问题是怎么判断相邻不能种,我们用2进制来表示每一行的种植情况.我们将每一行所能够造的所有可能都打表( ...
- POJ 3254 Corn Fields (状压DP,轮廓线DP)
题意: 有一个n*m的矩阵(0<n,m<=12),有部分的格子可种草,有部分不可种,问有多少种不同的种草方案(完全不种也可以算1种,对答案取模后输出)? 思路: 明显的状压DP啦,只是怎样 ...
- poj3254 Corn Fields (状压DP)
http://poj.org/problem?id=3254 Corn Fields Time Limit: 2000MS Memory Limit: 65536K Total Submissio ...
- P1879 [USACO06NOV]玉米田Corn Fields(状压dp)
P1879 [USACO06NOV]玉米田Corn Fields 状压dp水题 看到$n,m<=12$,肯定是状压鸭 先筛去所有不合法状态,蓝后用可行的状态跑一次dp就ok了 #include& ...
- POJ3254 Corn Fields(状压DP)
题目给个n×m的地图,1可以放玉米0不可以,现在要放玉米,玉米上下左右不能相邻,问放法有几种. 当前一行的决策只会影响下一行,所以状压DP之: dp[i][S]表示前i行放完且第i行放玉米的列的集合是 ...
- [USACO06NOV]玉米田$Corn \ \ Fields$ (状压$DP$)
#\(\mathcal{\color{red}{Description}}\) \(Link\) 农场主\(John\)新买了一块长方形的新牧场,这块牧场被划分成\(M\)行\(N\)列\((1 ≤ ...
- 【POJ3254】Corn Fields(状压DP)
题意: 一个M x N矩阵里有很多格子,每个格子有两种状态,可以放牧和不可以放牧,可以放牧用1表示,否则用0表示,在这块牧场放牛,要求两个相邻的方格不能同时放牛,即牛与牛不能相邻.问有多少种放牛方案( ...
随机推荐
- mongodb删除重复数据
注:mongodb当前版本是3.4.3 插入六条数据: 查询存在重复的数据: 查询并循环删除重复数据: 删除语句解析: db.userInfo.aggregate([ { ...
- Unity3D加密保护解决方案
精锐5加密锁支持Unity3D代码及资源保护,并提供授权方案 产品简介 可使用Virbox Protector加壳工具对Unity3D代码进行加密.Unity3D使用开源mono C#语法,代码会编译 ...
- kubectl批量删除pvc
#!/bin/bashkubectl get pvc |grep hub > tmp.txtcat tmp.txt |awk '{split($0,a," ");print ...
- Parco_Love_gcd
传送门 出题人说正解为RMQ,鄙人实在太蒟蒻了,不会呀只能暴力…… #include <bits/stdc++.h> using namespace std; #define ll lon ...
- Cocos Creator 使用计时器(官方文档摘录)
在 Cocos Creator 中,我们为组件提供了方便的计时器,这个计时器源自于 Cocos2d-x 中的 cc.Scheduler,我们将它保留在了 Cocos Creator 中并适配了基于组件 ...
- apache多站点vhost.conf配置
#第1个站点的配置 <VirtualHost *:80> DocumentRoot "D:/phpStudy/WWW" ServerName localhost < ...
- 正则表达式中test,match,exec区别
testtest 返回 Boolean,查找对应的字符串中是否存在模式.var str = "1a1b1c";var reg = new RegExp("1." ...
- KNN算法 - 数据挖掘算法(3)
(2017-04-10 银河统计) KNN算法即K Nearest Neighbor算法.这个算法是机器学习里面一个比较经典的.相对比较容易理解的算法.其中的K表示最接近自己的K个数据样本.KNN算法 ...
- URI/URL/URN的联系和区别
下面是我整理的一些关于他们的描述. URI,是uniform resource identifier,统一资源标识符,用来唯一的标识一个资源. 而URL是uniform resource locato ...
- Determine YARN and MapReduce Memory Configuration Settings
Determine YARN and MapReduce Memory Configuration Settings https://docs.hortonworks.com/HDPDocuments ...