poj 3254 Corn Field
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
Line 1: Two space-separated integers: M and N
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
Line 1: One integer: the number of ways that FJ can choose the squares modulo 100,000,000.
Sample Input
2 3
1 1 1
0 1 0
Sample Output
9
Hint
Number the squares as follows:
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
题意:农场主John新买了一块长方形的新牧场,这块牧场被划分成M行N列(1 ≤ M ≤ 12; 1 ≤ N ≤ 12),每一格都是一块正方形的土地。John打算在牧场上的某几格里种上美味的草,供他的奶牛们享用。
遗憾的是,有些土地相当贫瘠,不能用来种草。并且,奶牛们喜欢独占一块草地的感觉,于是John不会选择两块相邻的土地,也就是说,没有哪两块草地有公共边。
John想知道,如果不考虑草地的总块数,那么,一共有多少种种植方案可供他选择?(当然,把新牧场完全荒废也是一种方案)
。。。。。。。。。。。
此题看数据就知道是状压dp,f数组是记录每一行的状态是否存在贫瘠土地,mapp数组记录每一种状态是否有相邻的牛。
状态转移方程:dp[i][j]+=dp[i-1][k]; if(j&k==0)。
k是枚举出的上一行状态,j&k是保证两行没有相邻的牛。
#include<iostream>
#include<cstdio>
using namespace std;
const int N = 12;
const int mod = 100000000;
int dp[N+5][1<<N],m,n,a[N+5][N+5],ans,f[N+5];
bool mapp[1<<N];
int main() {
scanf("%d%d",&n,&m);
for(register int i=1; i<=n; i++)
for(register int j=1; j<=m; j++) {
scanf("%d",&a[i][j]);
f[i]=(f[i]<<1)+a[i][j];
}
for(register int i=0; i<1<<m; i++) {
bool flag=1;
int cnt=2;
for(register int j=0; j<m; j++) {
if(i>>j&1) {
if(cnt==1) {
flag=0;
break;
} else
cnt=1;
} else
cnt++;
}
if(flag) mapp[i]=1;
// cout<<i<<" "<<mapp[i]<<endl;
}
dp[0][0]=1;
for(register int i=1; i<=n; i++)
for(register int j=0; j<1<<m; j++) {
if(mapp[j] && ((f[i]&j)==j)) {
for(register int k=0; k<1<<m; k++)
if((j&k)==0){
dp[i][j]+=dp[i-1][k];
dp[i][j]%=mod;
}
}
}
for(register int i=0; i<1<<m; i++)
ans+=dp[n][i],ans%=mod;
ans%=mod;
printf("%d",ans);
}
poj 3254 Corn Field的更多相关文章
- 状压DP POJ 3254 Corn Fields
题目传送门 /* 状态压缩DP:先处理硬性条件即不能种植的,然后处理左右不相邻的, 接着就是相邻两行查询所有可行的种数并累加 写错一个地方差错N久:) 详细解释:http://www.tuicool. ...
- poj - 3254 - Corn Fields (状态压缩)
poj - 3254 - Corn Fields (状态压缩)超详细 参考了 @外出散步 的博客,在此基础上增加了说明 题意: 农夫有一块地,被划分为m行n列大小相等的格子,其中一些格子是可以放牧的( ...
- poj 3254 Corn Fields
http://poj.org/problem?id=3254 Corn Fields Time Limit: 2000MS Memory Limit: 65536K Total Submissio ...
- POJ 3254 Corn Fields(状压DP)
Corn Fields Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13732 Accepted: 7216 Desc ...
- poj 3254 Corn Fields 国家压缩dp
意甲冠军: 要在m行n陆行,有一些格您可以种树,别人做不到的.不相邻的树,我问了一些不同的共同拥有的法律. 分析: 从后往前种,子问题向父问题扩展,当种到某一格时仅仅有他和他后面的n-1个格子的情况对 ...
- POJ 3254. Corn Fields 状态压缩DP (入门级)
Corn Fields Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9806 Accepted: 5185 Descr ...
- POJ 3254 Corn Fields:网格密铺类 状压dp
题目链接:http://poj.org/problem?id=3254 题意: 给你一片n*m的耕地,你可以在上面种玉米.但是其中有一些地方是荒芜的,不能种植.并且种植玉米的地方不能相邻.问你在这片地 ...
- POJ 3254 - Corn Fields - [状压DP水题]
题目链接:http://poj.org/problem?id=3254 Time Limit: 2000MS Memory Limit: 65536K Description Farmer John ...
- POJ 3254 Corn Fields (状压dp)
题目链接:http://poj.org/problem?id=3254 给你n*m的菜地,其中1是可以种菜的,而菜与菜之间不能相邻.问有多少种情况. 状压dp入门题,将可以种菜的状态用一个数的二进制表 ...
随机推荐
- ajax json jQuery提示parsererror错误解决办法
$.ajax({ type:'POST', url:'<%=basePath%>/xxx.do', dataType:'JSON', data:{ }, success:function( ...
- Express post请求无法解析参数的原因
router.post('/', function(req, res) { console.log(req.body); console.log(req.body.name); console.log ...
- Vue学习笔记【18】——Vue中的动画(使用过渡类名)
为什么要有动画:动画能够提高用户的体验,帮助用户更好的理解页面中的功能: 使用过渡类名 步骤分析 需求: 点击按钮,让 h3 显示,再点击,让 h3 隐藏 1. 使用 transition 元素, ...
- 每天一个Linux命令:ls(1)
ls ls命令用于显示指定工作目录下之内容(列出目前工作目录所含之文件及子目录). 格式 ls [-alrtAFR] [name...] 参数选项 参数 备注 -a 列出目录下的所有文件,包括以 . ...
- session控制登入权限
<?php session_start(); if(empty($_SESSION["uid"]))//判断SESSION是不是为空 { header("locat ...
- jQuery选择器 (详解)
1. 基础选择器 Basics 名称 说明 举例 #id 根据元素Id选择 $("divId") 选择ID为divId的元素 element 根据元素的名称选择, $(" ...
- 机器学习中python的有关使用技巧【创建虚拟环境、jupyter的kernel修改】
1.创建虚拟环境<在原来基础上建立> *注:(这里是python2.python3环境共存,我要创建一个python3的虚拟环境) 一.先安装虚拟环境变量: pip3 install -U ...
- PHP面试 PHP基础知识 十(网络协议)
网络协议 HTTP协议状态码 状态分为五大类:1XX.2XX.3XX.4XX.5XX 1XX:信息类状态码 表示接受请求正在处理 2XX:success 成功状态码 请求正常处理完毕 3XX:重定 ...
- PAT_A1088#Rational Arithmetic
Source: PAT A1088 Rational Arithmetic (20 分) Description: For two rational numbers, your task is to ...
- flex上下、左右居中
tip:1)flex是用于div布局用的,作用于一级子元素(父元素写样式,作用于子元素) 2)弹性盒模型 3)英文解释justify-content: 对齐内容(内容一般写在主轴上)align-ite ...