描述

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.

输入

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)

输出

Line 1: One integer: the number of ways that FJ can choose the squares modulo 100,000,000.

样例输入

2 3

1 1 1

0 1 0

样例输出

9

提示

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.

题意
一块M*N的田用于养牛,1代表可以养牛,要求每头牛上下左右没有其他牛,问你方案数模1e9
题解
状压dp经典题,这里用样例解释
首先把列压成01串状态,1表示有牛,0表示无牛,那么总共有[000,111]总状态,000,001,010,011,100,101,110,111
dp[i][j]代表第i行可行状态j的方案总数,可行状态j的意思是没有两个连续的1,所以011和110和111去掉
state[i]代表可行的状态,所以只有000,001,010,100,101五种状态,此时所有可行状态数tot=5
cur[i]代表样例行的状态取反,取反是为了判断当前行枚举的五种状态是否可行,例如第二行题目是010,取反后是101,也就是说只有000,010两种状态可行
 
状态转移方程
if      j∈[1,tot],state[j]&cur[i]=0,k∈[1,tot],state[k]&cur[i-1]=0,state[j]&state[k]=0
dp[i][j]=dp[i][j]+dp[i-1][k]表示当前行i状态j可由前一行i-1状态k转移过来
else
dp[i][j]=dp[i][j];
代码

 #include<stdio.h>
#include<string.h>
using namespace std; const int mod=1e9; int dp[][],state[],cur[];
int n,m,k,tot;
int main()
{
while(scanf("%d%d",&m,&n)!=EOF)
{
tot=;
for(int i=;i<(<<n);i++)///列的所有可行状态
if(!(i&(i<<)))///没有连续两个1
state[++tot]=i; for(int i=;i<=m;i++)
for(int j=;j<=n;j++)
{
scanf("%d",&k);
if(!k)cur[i]+=<<(n-j);///第i行不可行置1,用于判断不可行状态
} for(int i=;i<=tot;i++)
if(!(state[i]&cur[]))
dp[][i]=;
for(int i=;i<=m;i++)
{
for(int j=;j<=tot;j++)///枚举当前状态
{
if(state[j]&cur[i])continue;///当前状态不可行
for(int k=;k<=tot;k++)///枚举前一个状态
{
if(state[k]&cur[i-])continue;///当前前一状态不可行
if(state[j]&state[k])continue;///当前状态和前一状态不可行
dp[i][j]=(dp[i][j]+dp[i-][k])%mod;///当前状态可以从前一状态的K状态转移过来
}
}
}
int ans=;
for(int i=;i<=tot;i++)
ans=(ans+dp[m][i])%mod;
printf("%d\n",ans);
}
return ;
}

POJ 1684 Corn Fields(状压dp)的更多相关文章

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

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

  2. POJ 3254 Corn Fields (状压dp)

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

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

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

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

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

  5. poj 3254 Corn Fields 状压dp入门

    题目链接 题意 在\(M\times N\)的\(0,1\)格子上放东西,只有标记为\(1\)的格子可以放东西,且相邻的格子不能同时放东西.问有多少种放法. 思路 参考:swallowblank. \ ...

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

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

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

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

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

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

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

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

随机推荐

  1. APP-7-百度地图移动轨迹

    1.代码部分 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <me ...

  2. jinjia

    https://www.cnblogs.com/dachenzi/p/8242713.html

  3. iOS 内存管理,ARC

    iOS 对象内存释放时机:当对象的引用计数为0时对象被释放. 所以如下代码: __weak NSObject * a = [[NSObject alloc] init]; 这个对象在创建完赋完值后会被 ...

  4. Delphi 泛型详解

    http://www.cnblogs.com/jxgxy/category/216671.html

  5. 高并发架构技术|缓存失效、缓存穿透问题 PHP 代码解决

    问题描述 缓存失效: 引起这个原因的主要因素是高并发下,我们一般设定一个缓存的过期时间时,可能有一些会设置5分钟啊,10分钟这些:并发很高时可能会出在某一个时间同时生成了很多的缓存,并且过期时间在同一 ...

  6. Python 数据结构基本操作

    数据结构是用来存储数据的逻辑结构,合理使用数据结构才能编写出优秀的代码.本文主要介绍Python提供的几种内置数据结构,包括元组.列表.字典的定义和基本操作方法以及介绍典型函数的使用方法. 元组结构 ...

  7. Java如何创建参数个数不限的函数

    可变的参数类型,也称为不定参数类型.英文缩写是varargus,还原一下就是variable argument type.通过它的名字可以很直接地看出来,这个方法在接收参数的时候,个数是不定的. pu ...

  8. pycharm 配置服务器,脚本,测试文件

    配置django服务器 我们配置在用pycharm开发的时候,一般都是习惯于python manage.py runserver 0.0.0.0:80000 这种方式,但是,该方式并不能进入debug ...

  9. EF AutoMaper

    Mapper.CreateMap<Source,Dest>(); 该方法已弃用,使用下面这个 Mapper.Initialize(x=>x.CreateMap<Source,D ...

  10. Html写作规范

    HTML是描述网页结构的超文本标记语言,HTML规范能够使HTML代码风格保持一致,使得HTML更容易理解和维护. 整体结构 用编辑器快捷键一键搞定 <!DOCTYPE html>---- ...