poj3254 Corn Fields (状压DP)
http://poj.org/problem?id=3254
|
Corn Fields
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 Sample Output 9 Hint Number the squares as follows:
1 2 3 There are four ways to plant only on one squares (1, 2, 3, or 4), Source |
大意:有一个m行n列的矩阵,里面为1的地方可以种玉米,为0的地方不能种玉米。玉米不能种在相邻的格子(种玉米的格子不能有共用边),求种玉米的种类数。模100000000。
题解:状压DP。
状压DP一般是把状态压到1个数里,然后这个数可以当数组的下标。这题就是把一行当一个状态,用一个数的12个二进制位表示这一行各个地有没有种玉米,2^12=4096,随便接受。
由于玉米不能相邻,我们可以预处理排除很多状态,只用可能的状态。用x表示状态,则(x&(x<<1))为真就是有玉米相邻,不用这种状态。最后把状态存到state[]里,n=12时也只有不到400个。
把一行各个格子是否允许种玉米也压成一个数y,(x&y)为真就是这行不能用x这个状态。
f[i][j]表示:第i行的状态为j,0~i-1行有多少种方案。
一行一行推,枚举这一行的状态j、上一行的状态l(这里的j和l是状态编号,不是状态的二进制),若(state[j] & state[l])为真,说明有两行有相邻的,continue。
一行一行累计种类数,把最后一行各个状态的种类数求和就能得到答案,记得取模。
我怕运算过程中产生超int,所以过程中转成long long。其实也可以直接全部用long long变量就行。
代码:
//#pragma comment(linker, "/STACK:102400000,102400000")
#include<cstdio>
#include<cmath>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
#define ll long long const int MOD=; int a[];
int state[], sn;
int f[][];
int m,n; void init(){
sn=;
int maxi=<<n,i;
memset(state,,sizeof(state));
for(i=;i<maxi;i++){
if(i&(i<<))continue;
state[sn++]=i;
}
} int main(){
int i,j,l,x,y;
while(scanf("%d%d",&m,&n)!=EOF){
for(i=;i<m;i++){
y=;
for(j=;j<n;j++){
scanf("%d",&x);
y|=x<<j;
}
a[i]=y;
}
init();
memset(f,,sizeof(f));
for(i=;i<sn;i++)
if((state[i]&a[]) == state[i]) f[][i]=;
for(i=;i<m;i++){
for(j=;j<sn;j++){
if((state[j]&a[i]) != state[j]) continue;
for(l=;l<sn;l++){
if(state[j]&state[l])continue;
f[i][j]=((ll)f[i][j]+(ll)f[i-][l])%MOD;
}
//printf("i=%d,state[j]=%x,f[i][j]=%d\n",i,state[j],f[i][j]);
}
}
int ans=;
for(i=;i<sn;i++){
ans=((ll)ans+(ll)f[m-][i])%MOD;
}
printf("%d\n",ans);
}
return ;
}
poj3254 Corn Fields (状压DP)的更多相关文章
- 【POJ3254】Corn Fields 状压DP第一次
!!!!!!! 第一次学状压DP,其实就是运用位运算来实现一些比较,挺神奇的.. 为什么要发“!!!”因为!x&y和!(x&y)..感受一下.. #include <iostre ...
- POJ 1684 Corn Fields(状压dp)
描述 Farmer John has purchased a lush new rectangular pasture composed of M by N (1 ≤ M ≤ 12; 1 ≤ N ≤ ...
- 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入门题,将可以种菜的状态用一个数的二进制表 ...
- [ An Ac a Day ^_^ ] POJ 3254 Corn Fields 状压dp
题意: 有一块n*m的土地 0代表不肥沃不可以放牛 1代表肥沃可以放牛 且相邻的草地不能同时放牛 问最多有多少种放牛的方法并对1e8取模 思路: 典型的状压dp 能状态压缩 能状态转移 能状态压缩的题 ...
- P1879 [USACO06NOV]玉米田Corn Fields 状压dp/插头dp
正解:状压dp/插头dp 解题报告: 链接! ……我真的太菜了……我以为一个小时前要搞完的题目调错误调了一个小时……90分到100我差不多搞了一个小时…… 然后这题还是做过的……就很气,觉得确实是要搞 ...
- [USACO06NOV]玉米田Corn Fields 状压DP
题面: 农场主John新买了一块长方形的新牧场,这块牧场被划分成M行N列(1 ≤ M ≤ 12; 1 ≤ N ≤ 12),每一格都是一块正方形的土地.John打算在牧场上的某几格里种上美味的草,供他的 ...
- [USACO06NOV]玉米田Corn Fields (状压$dp$)
题目链接 Solution 状压 \(dp\) . \(f[i][j][k]\) 代表前 \(i\) 列中 , 已经安置 \(j\) 块草皮,且最后一位状态为 \(k\) . 同时多记录一个每一列中的 ...
- [poj3254]Corn Fields_状压dp
Corn Fields poj3254 题目大意:给你一个n*m的地,每一块地可以种或不种,两块种过的地不能挨着,可以一块都不种,问所有的种地方案数. 注释:读入用0和1,1<=n,m<= ...
- Poj - 3254 Corn Fields (状压DP)(入门)
题目链接:https://vjudge.net/contest/224636#problem/G 转载于:https://blog.csdn.net/harrypoirot/article/detai ...
随机推荐
- bzoj1396: 识别子串
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #i ...
- css后代选择器(div.class中间不带空格)
如果我要查找<div>上用了.class的元素,查找方法:div.class:中间是不空格的. 以上这种形式为css后代选择器 参考:http://www.w3school.com.cn/ ...
- TrustedInstaller管理权限
TrustedInstaller.exe实际上是“Windows Modules Installer”这个服务的进程,路径位于C:\Windows\servicing\TrustedInstaller ...
- [iOS 利用MapKit和CoreLocation框架打造精简的定位和导航]
运行效果: 一.利用<CoreLocation/CoreLocation.h>定位 创建变量 CLLocationManager *locationManager , ...
- pack、unpack自制二进制“数据库”
引言 pack.unpack函数,如果没有接触过socket,这个可能会比较陌生,这两个函数在socket交互的作用是组包,将数据装进一个二进制字符串,和对二进制字符串中的数据进行解包,这个里面有好多 ...
- (原)list中null的谨慎使用
今天在刷算法题时,新建了ArrayList变量,发现ArrayList与null结合起来使用时经常出错. 请查看如下几种例子, 1.new一个ArrayList<>类型的数据, impor ...
- LaTeX 笔记---Q&A
Q: Is it possible to check which package do I need to install to get certain .sty file from tlmgr ra ...
- 《C陷阱与缺陷》杂记
第一章 词法"陷阱" 1.4整型常量 如果一个整型常量的第一个字符是数字0,那么该常量将被视作八进制数.因此,10与010的含义截然不同.需要注意这种情况,有时候在上下文为了格式& ...
- hdu 2036 - 改革春风吹满地(计算几何)
题意:求解多边形面积 解法: 先了解数学上"叉积"的含义与性质: 三角形ΔABC的面积为: 我们可以依次计算每个三角形的面积,ΔABC,ΔACE,ΔEF - - 所有三角形的面积之 ...
- HBase filter shell操作
创建表 create 'test1', 'lf', 'sf' lf: column family of LONG values (binary value) -- sf: column family ...