描述

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. wget 递归下载整个网站(网站扒皮必备)

    有时间看到别人网站的页面比较漂亮,就想给扒皮下来,学习学习.分享一个我常用网站扒皮命令wget 这个命令可以以递归的方式下载整站,并可以将下载的页面中的链接转换为本地链接. wget加上参数之后,即可 ...

  2. FB4.6项目迁移到4.7时 embed报错问题

    问题: 从FB4.6或更早版本移植到4.7的项目Embed标签,比如 [Embed(source="assets/BtnPlay.png")]   ,会报错 解决 方案: 4.7E ...

  3. setTimeout闭包常见问题

    经常会遇到这样的问题,setTimeout按序输出循环数字,而不是最后输出同一个数字: 题目: for (var i = 0; i < 5; i++) { setTimeout(function ...

  4. 130. Surrounded Regions 卧槽!我半梦半醒之间做出来的。

    打开这个题,做了一半躺下了. 结果,怎么都睡不着.一会一个想法,忍不住爬起来提交,要么错误,要么超时. 按照常规思路,依次对每个点检测是否是闭包,再替换,超时.计算量太大了. 还能怎么做呢?没思路,关 ...

  5. C# 2018.9.17

    C#的优点:1,不会有运行时崩溃,解决了C++的痛点一,难预防,难查错2,使用文件不需要包含进来,只需要using namespace即可,解决了C++的痛点二,包含复杂,路径复杂,编译复杂3,编译速 ...

  6. Unable to connect to zookeeper server within timeout: 5000

    错误 严重: StandardWrapper.Throwable org.springframework.beans.factory.BeanCreationException: Error crea ...

  7. Tomcat运行javaweb项目时出现的一个bug

    Stacktrace:with root cause java.net.ConnectException: Connection refused:........................... ...

  8. 24.Hibernate-各种类型的查询.md

    目录 1.查询的类别 2.实例 1.查询的类别 按照查询的方式分为面向对象和非面向对象 面向对象 HQL查询:面向对象方式,可以跨数据库,但是需要SQL基础.最常用的方式.查询的对象是类和类的属性,不 ...

  9. eclipse git 删除内容

  10. Oracle SCN机制解析

    SCN(System Chang Number)作为oracle中的一个重要机制,在数据恢复.Data Guard.Streams复制.RAC节点间的同步等各个功能中起着重要作用.理解SCN的运作机制 ...