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入门题,将可以种菜的状态用一个数的二进制表 ...
随机推荐
- springboot集成使用rabbitmq笔记(1.rabbitmq安装)
使用rabbitmq笔记一 使用rabbitmq笔记二 使用rabbitmq笔记三 1.选择适配的版本,参考---https://www.rabbitmq.com/which-erlang.html ...
- 线段树区间合并——cf1217E
/* 定义:多重集s的元素和是sum,对于sum的第i位为x,如果s里找不到第i位也是x的元素,那么称这个多重集为bad, 现在给定一个 序列a 两种操作 1 i x:将序列第i个元素改为x 2 l ...
- 埃氏筛+线段树——cf731F
从2e5-1依次枚举每个数作为主显卡,然后分段求比它大的数的个数,这里的复杂度是调和级数ln2e5,即埃氏筛的复杂度.. #include<bits/stdc++.h> using nam ...
- AcWing 156. 矩阵 (哈希二维转一维查询)打卡
给定一个M行N列的01矩阵(只包含数字0或1的矩阵),再执行Q次询问,每次询问给出一个A行B列的01矩阵,求该矩阵是否在原矩阵中出现过. 输入格式 第一行四个整数M,N,A,B. 接下来一个M行N列的 ...
- js面向对象编程(第2版)——js继承多种方式
附带书籍地址: js面向对象编程(第2版)
- Android开发常用的Intent的URI及示例
参考资料:http://www.oschina.net/code/snippet_166763_6502 //以下是常用到的Intent的URI及其示例,包含了大部分应用中用到的共用Intent. / ...
- 牛客Another Distinct Values
题目 我一开始试了一下n=3,发现不行,于是就猜测只有2可以,交了一发,通过了50%.所以我猜测只有偶数可以,奇数都不行. 中间空白部分放n=2的情况就行.因为n=2的情况时,最大只有2,所以我们可以 ...
- C/S模式简单socket通信
TCP连接方式 sever.c #include <stdio.h>#include <stdlib.h>#include <sys/socket.h>#inclu ...
- PHP面试 PHP基础知识 七(文件及目录处理)
文件操作 文件打开函数 fopen()函数 //用来打开一个文件 打开时需要指定打开模式 语法:fopen( filename, mode, include_path, context); filen ...
- PostgreSQL——服务器配置_{postgresql.conf}
一.设置参数 所有参数名称都是不区分大小写的 值为字符串时,需要单引号 值为数值时不需要单引号,但带单位时,需要单引号 配置文件(如:postgresql.conf.postgresql.auto.c ...