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表示,在这块牧场放牛,要求两个相邻的方格不能同时放牛,即牛与牛不能相邻.问有多少种放牛方案( ...
随机推荐
- sql server创建windows账户
--不要干坏事 sql server中使用xp_cmdshell --1.允许配置高级选项 GO RECONFIGURE GO --2.开启xp_cmdshell服务 RECONFIGURE GO - ...
- python之以字符串形式导入模块
示例 调用方法 class CorsMiddleware: def process(self): print('from auth.cors.CorsMiddleware.process') cors ...
- 有没有无痛无害的人体成像方法?OCT(光学相干断层扫描)了解一下
关于之前推送的胸片和CT有很多的小伙伴关心射线对人体的伤害的问题,在医学检查射线的强度和剂量已经有严格的标准,偶尔进行一次CT扫描是没有问题的,那么有没有一种完全无害的扫描检查呢?今天小编就给大家介绍 ...
- Spring框架第二天
## Spring框架第二天 ## ---------- **课程回顾:Spring框架第一天** 1. 概述 * IOC和AOP 2. 框架的IOC的入门 * 创建applicationContex ...
- 记oracle使用expdp将数据导出到asm报错
报错信息如下: ORA-39002: invalid operationORA-39070: Unable to open the log file.ORA-29283: invalid file o ...
- 通用订单搜索的API设计得失录
先把 Joshua Bloch 大神的 API PDF 放在这里膜拜下:"How to Design a Good API and Why it Matters.pdf" 总述 在 ...
- Applet程序组件与AJAX技术
Applet 定义 Applet是一种运行于Web客户端环境下的Java程序组件. 工作原理 Applet以代码的形式嵌入Web页面中,用标签<applet></applet> ...
- Python多线程threading的使用
一. threading的参数传递,参数之后的’,‘不能少,此处的’,‘是用来区分此参数作为元组(包含多个参数)来传递的,而不是单个参数传递 #coding:utf- import threading ...
- 看懂MSSQL执行计划,分析SQL语句执行情况
打开SQL执行计划窗口 执行计划的图表是从右向左看的 SQL Server有几种方式查找数据记录 [Table Scan] 表扫描(最慢),对表记录逐行进行检查 [Clustered Index Sc ...
- 算法(第四版)C# 习题题解——1.1
写在前面 整个项目都托管在了 Github 上:https://github.com/ikesnowy/Algorithms-4th-Edition-in-Csharp 善用 Ctrl + F 查找题 ...