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 ...
随机推荐
- wrapper for lua
考虑使用已经有的dll,要写wrapper,使得在lua中能调用dll里的函数,嗯,参考<Programming in lua>,然后仿写luars232. 一.函数定义 先分析一个函数的 ...
- 数据结构算法C语言实现(十九)--- 5.5&5.6&5.7广义表
一.简述 传说Lisp的基本数据结构就是广义表,广义表也是具有典型递归属性的数据结构,此外,由于建表要处理字符串,用C语言处理起来也是一脸懵逼.....最后自己还想写一个将广义表还原成字符串的函数,一 ...
- JUnit报错:java.lang.ClassNotFoundException:
只要把Java--------compiler-------building-------Buil path problems ------- incomplete build path 和 Circ ...
- MVC设计模式与三层架构
三层架构分别是:表示层(Web层).业务逻辑层(BLL层)和数据访问层(DAL层). (1)表示层负责: a.从用户端收集信息 b.将用户信息发送到业务服务层做处理 c.从业务服务层接收处理结果 d. ...
- AngularJs ngHref、ngSrc、ngCopy/ngCut/ngPaste
ngHref 在Angular程序没完成改变链接上用{{hash}}方式绑定的href值的时候,当用户点击该链接会跳到一个错误的页面. 格式:ng-href=”value” value:表达式. 使用 ...
- Java反射API使用实例
/** * 访问Class对应的类所包含的注释:getAnnotation();getDelaredAnnotation(); * 访问Class对应的类所包含的内部类:getDecl ...
- POJ - Til the Cows Come Home(Dijkstra)
题意: 有N个点,给出从a点到b点的距离,当然a和b是互相可以抵达的,问从1到n的最短距离 分析: 典型的模板题,但是一定要注意有重边,因此需要对输入数据加以判断,保存较短的边,这样才能正确使用模板. ...
- 20个命令行工具监控Linux系统性能
作为Linux/Unix 系统管理员需要掌握一些常用的工具用于检测系统性能.在这里,dodo为大家推荐非常20个有用的并且最常用的命令行系统监视工具: 1. top -Linux系统进程监控 top ...
- 使用chrome查看页面元素的css样式
使用chrome查看页面元素的css样式 我们在写页面的时候,可能总是不知道怎么回事,就发现某个元素出现了问题,但是不知道为什么会这样,这时候,就需要使用强大的chrome了!!! 举例说明: 1. ...
- base64 convert to file
var fs= require('fs') var imageFile = dataUrl.replace(/^data:image\/\w+;base64,/, ""); var ...