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)的更多相关文章

  1. poj 3254 Corn Fields (状压dp)(棋盘dp)

    状压dp入门题 因为当前行的状态只和上一行有关 所以可以一行一行来做 因为m <= 12所以可以用二进制来表示放了或者没有放 0表示没放,1表示放 f[i][state]表示第i行状态为stat ...

  2. 动态规划晋级——POJ 3254 Corn Fields【状压DP】

    转载请注明出处:http://blog.csdn.net/a1dark 分析:刚开始学状压DP比较困难.多看看就发现其实也没有想象中那么难.这道题由于列数较小.所以将行压缩成二进制来看.首先处理第一行 ...

  3. POJ - 3254 Corn Fields(状压DP)题解

    思路: 参照blog,用状压DP做,和题解稍微有点不一样,我这里直接储存了状态而不是索引. 这一题的问题是怎么判断相邻不能种,我们用2进制来表示每一行的种植情况.我们将每一行所能够造的所有可能都打表( ...

  4. POJ 3254 Corn Fields (状压DP,轮廓线DP)

    题意: 有一个n*m的矩阵(0<n,m<=12),有部分的格子可种草,有部分不可种,问有多少种不同的种草方案(完全不种也可以算1种,对答案取模后输出)? 思路: 明显的状压DP啦,只是怎样 ...

  5. poj3254 Corn Fields (状压DP)

    http://poj.org/problem?id=3254 Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissio ...

  6. P1879 [USACO06NOV]玉米田Corn Fields(状压dp)

    P1879 [USACO06NOV]玉米田Corn Fields 状压dp水题 看到$n,m<=12$,肯定是状压鸭 先筛去所有不合法状态,蓝后用可行的状态跑一次dp就ok了 #include& ...

  7. POJ3254 Corn Fields(状压DP)

    题目给个n×m的地图,1可以放玉米0不可以,现在要放玉米,玉米上下左右不能相邻,问放法有几种. 当前一行的决策只会影响下一行,所以状压DP之: dp[i][S]表示前i行放完且第i行放玉米的列的集合是 ...

  8. [USACO06NOV]玉米田$Corn \ \ Fields$ (状压$DP$)

    #\(\mathcal{\color{red}{Description}}\) \(Link\) 农场主\(John\)新买了一块长方形的新牧场,这块牧场被划分成\(M\)行\(N\)列\((1 ≤ ...

  9. 【POJ3254】Corn Fields(状压DP)

    题意: 一个M x N矩阵里有很多格子,每个格子有两种状态,可以放牧和不可以放牧,可以放牧用1表示,否则用0表示,在这块牧场放牛,要求两个相邻的方格不能同时放牛,即牛与牛不能相邻.问有多少种放牛方案( ...

随机推荐

  1. 前端的图片压缩image-compressor(可在图片上传前实现图片压缩)

    https://www.imooc.com/article/40038 https://www.jianshu.com/p/3ce3e3865ae2 前端的图片压缩image-compressor(可 ...

  2. Python基础(八) yaml在python中的使用

    yaml 通常用来存储数据,类似于json YAML 简介 YAML(Yet Another Markup Language),一种直观的能够被电脑识别的数据序列化格式,是一个可读性高并且容易被人类阅 ...

  3. 日志采集器windows客户端的配置释义

    <Extension json> Module xm_json </Extension> <Extension charconv> Module xm_charco ...

  4. CentOS 7 系统优化

    系统调优4大子系统 1:找出系统中使用CPU最多的进程 2:找出系统中使用内存最多的进程 3:找出系统中对磁盘读写最多的进程 4:找出系统中使用网络最多的进程 系统调优概述 系统的运行状况:  CPU ...

  5. 19 Python标准异常总结 (转)

    Python标准异常总结 AssertionError 断言语句(assert)失败 AttributeError 尝试访问未知的对象属性 EOFError 用户输入文件末尾标志EOF(Ctrl+d) ...

  6. BeautifulReport--适用于unittest自动化测试的可视化报告

    安装: 因为是由大神分享的,可以直接在github<https://github.com/TesterlifeRaymond/BeautifulReport>上下载 git clone g ...

  7. GoldenGate 12.3 MA架构介绍系列(5) - 静默安装

    软件下载地址 http://www.oracle.com/technetwork/middleware/goldengate/downloads/index.html 下载基于MicroService ...

  8. sed 正则的一个小问题

    有一段类似以下的文本 aabbccc test[3307]112323553-66778tp aooppx69tp ooppsg aabbccc test[3307]1127233-6674tp bo ...

  9. CSS 层级选择器

    CSS 层级选择器 注:使用空格分隔,塞选到最后一项应用. 标签选择器 下 标签选择器 父标签 --> 指定子标签添加css属性 <html> <head> <!- ...

  10. Docker+Consul+Registrator 实现服务注册与发现

    Docker+Consul+Registrator实现服务注册与发现 逻辑图 实现nginx节点自动化加入容器IP代理 1.三台Consul agent server作为高可用通过Consul Tem ...