http://poj.org/problem?id=3254

Corn Fields
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 7588   Accepted: 4050

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

大意:有一个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)的更多相关文章

  1. 【POJ3254】Corn Fields 状压DP第一次

    !!!!!!! 第一次学状压DP,其实就是运用位运算来实现一些比较,挺神奇的.. 为什么要发“!!!”因为!x&y和!(x&y)..感受一下.. #include <iostre ...

  2. POJ 1684 Corn Fields(状压dp)

    描述 Farmer John has purchased a lush new rectangular pasture composed of M by N (1 ≤ M ≤ 12; 1 ≤ N ≤ ...

  3. POJ 3254 - Corn Fields - [状压DP水题]

    题目链接:http://poj.org/problem?id=3254 Time Limit: 2000MS Memory Limit: 65536K Description Farmer John ...

  4. POJ 3254 Corn Fields (状压dp)

    题目链接:http://poj.org/problem?id=3254 给你n*m的菜地,其中1是可以种菜的,而菜与菜之间不能相邻.问有多少种情况. 状压dp入门题,将可以种菜的状态用一个数的二进制表 ...

  5. [ An Ac a Day ^_^ ] POJ 3254 Corn Fields 状压dp

    题意: 有一块n*m的土地 0代表不肥沃不可以放牛 1代表肥沃可以放牛 且相邻的草地不能同时放牛 问最多有多少种放牛的方法并对1e8取模 思路: 典型的状压dp 能状态压缩 能状态转移 能状态压缩的题 ...

  6. P1879 [USACO06NOV]玉米田Corn Fields 状压dp/插头dp

    正解:状压dp/插头dp 解题报告: 链接! ……我真的太菜了……我以为一个小时前要搞完的题目调错误调了一个小时……90分到100我差不多搞了一个小时…… 然后这题还是做过的……就很气,觉得确实是要搞 ...

  7. [USACO06NOV]玉米田Corn Fields 状压DP

    题面: 农场主John新买了一块长方形的新牧场,这块牧场被划分成M行N列(1 ≤ M ≤ 12; 1 ≤ N ≤ 12),每一格都是一块正方形的土地.John打算在牧场上的某几格里种上美味的草,供他的 ...

  8. [USACO06NOV]玉米田Corn Fields (状压$dp$)

    题目链接 Solution 状压 \(dp\) . \(f[i][j][k]\) 代表前 \(i\) 列中 , 已经安置 \(j\) 块草皮,且最后一位状态为 \(k\) . 同时多记录一个每一列中的 ...

  9. [poj3254]Corn Fields_状压dp

    Corn Fields poj3254 题目大意:给你一个n*m的地,每一块地可以种或不种,两块种过的地不能挨着,可以一块都不种,问所有的种地方案数. 注释:读入用0和1,1<=n,m<= ...

  10. Poj - 3254 Corn Fields (状压DP)(入门)

    题目链接:https://vjudge.net/contest/224636#problem/G 转载于:https://blog.csdn.net/harrypoirot/article/detai ...

随机推荐

  1. 【BZOJ-3910】火车 倍增LCA + 并查集

    3910: 火车 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 262  Solved: 90[Submit][Status][Discuss] De ...

  2. jdbc/ojdbc连oracle的三种方式(转)

    文章转自:http://blog.itpub.net/22664653/viewspace-1383092/ 前言  本文是一篇学习笔记,学习如何通过java jdbc /ojdbc 连接oracle ...

  3. 关于Android程序设计—有道词典demo转移至coding的公告

    有道词典的demo以及解析已转移至Coding https://coding.net/u/monsterLin/p/WebView_YouDao/git

  4. php面向对象中static静态属性和静态方法的调用

    这篇文章主要介绍了php面向对象中static静态属性和静态方法的调用,实例分析了static静态属性和静态方法的原理与调用技巧,需要的朋友可以参考下 本文实例讲述了php中static静态属性和静态 ...

  5. Codeforces Round #389 Div.2 A. Santa Claus and a Place in a Class

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

  6. Akka: actor应用的一些小结

    1.消息: 1) case class是scala中一个不可变对象(当然你可以让他成为可变的),通过不可变对象来进行消息传递可以更加明确内容,也能保证线程安全 2) 在Java中如果你将class对象 ...

  7. array数组加过滤

    var array = new Array(); array.push(0); array.push(1); array.push(2); var arr = array.filter(functio ...

  8. 数据结构算法C语言实现(七)--- 3.1栈的线性实现及应用举例

    一.简述 栈,LIFO.是操作受限的线性表,和线性表一样有两种存储表示方法.下面以顺序存储为例,实现. 二.ADT 暂无. 三.头文件 //3_1.h /** author:zhaoyu email: ...

  9. [Android]Volley源码分析(二)

    上一篇介绍了Volley的使用,主要接触了Request与RequestQueue这两个类,这篇就来了解一下这两个类的具体实现. Request类图:

  10. Ubuntu学习总结-02 Ubuntu下的FTP服务的安装和设置

    一 安装vsftpd 在安装前vsftpd,先更新apt-get下载的数据源输入如下命令: sudo apt-get update 然后安装vsftpd sudo apt-get install vs ...