Corn Fields
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 13732   Accepted: 7216

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.

题目链接:POJ 3254

做的第二道状态压缩DP题,按自己前一道的思路写的,写的比较慢但是过程感觉很清晰,修改了很多次,最后0MS过了还是不错的

这题跟做过的其他入门题有一点不同,就是他不仅要不相邻(可以同一列),因此第$i$行怎么放不影响第$i-2$,$i+2$行,因此就不需要用或进行状态叠加,显然状压基本法第一步就是初始化,把第一行的数据初始化,

所以为了方便和快速遍历所有本身不相邻的状态,先预处理把状态存到fit里,有tot个,然后枚举每一个状态看是否能放到这一行草地上,我就偷个懒直接用bitset for一遍判断——若当前不是草地但状态里却有一个牛显然这就不合法不能放进草地,否则$dp[0][当前十进制状态]=1$。

然后按照基本步骤先遍历每一行,再枚举上一行$k$和这一行的状态$j$,若上一行$j$可以放草地且这一行$k$也可以放草地且$k$与$j$不冲突则$dp[i][j]=dp[i][j]+dp[i-1][k]$

最后还是枚举存好的状态(不需要枚举1<<m次因为最后一行肯定也是合法的一定全在保存好的fit里面)把状态相加,最后想了一下为什么不是把每一行的每一种状态都加起来呢?因为你状态从$dp[i-1][k]$转移到$dp[i][j]$用的是+=已经把前面的状态算上去了。

最后送一组测试数据

3 3
1 1 1
1 0 1
1 1 1

答案是47

代码:

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <sstream>
#include <cstring>
#include <bitset>
#include <string>
#include <deque>
#include <stack>
#include <cmath>
#include <queue>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
typedef pair<int,int> pii;
typedef long long LL;
const double PI=acos(-1.0);
const int N=14;
const int mod=100000000;
int pos[N][N],Grass[N];
int dp[N][1<<N];
int fit[1<<N];
int tot,n,m,R; bool check(const int &a,const int &b)
{
return (a&b)==0;
}
bool check_grass(const int &grass,const int &v)
{
bitset<N> bg=grass,bv=v;
for (int i=0; i<N; ++i)
if(bg[i]==0&&bv[i]==1)
return false;
return true;
}
void init()
{
CLR(dp,0);
CLR(Grass,0); tot=0;
R=1<<m;
for (int i=0; i<R; ++i)
if(check(i,i<<1))
fit[tot++]=i;
}
int main(void)
{
int i,j,k;
while (~scanf("%d%d",&n,&m))
{
init();
for (i=0; i<n; ++i)
{
int bis=0;
for (j=0; j<m; ++j)
{
scanf("%d",&pos[i][j]);
bis=(bis<<1)+pos[i][j];
}
Grass[i]=bis;
} for (i=0; i<tot; ++i)
if(check_grass(Grass[0],fit[i]))
dp[0][fit[i]]=1; for (i=1; i<n; ++i)
{
for (j=0; j<tot; ++j)//第i行
{
if(check_grass(Grass[i],fit[j]))
{
for (k=0; k<tot; ++k)//枚举第i-1行
{
if(check_grass(Grass[i-1],fit[k])&&check(fit[k],fit[j]))//i-1行可放且与第i行不冲突
dp[i][fit[j]]+=dp[i-1][fit[k]];
}
}
}
}
int cnt=0;
for (i=0; i<tot; ++i)
{
cnt+=dp[n-1][fit[i]];
cnt%=mod;
}
printf("%d\n",cnt);
}
return 0;
}

POJ 3254 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. POJ 1684 Corn Fields(状压dp)

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

  7. POJ 3254 Corn Fields (状压入门)

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

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

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

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

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

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

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

随机推荐

  1. Android实现监测网络状态

    本文主要用到了安卓监测网络状态变化功能,实现了WIFI,3G,无网络状态切换时发出通知的功能. 主要知识点 service broadcast 接口回调实现 service的基本知识 service可 ...

  2. Java Hour 30 Weather ( 3 )

    有句名言,叫做10000小时成为某一个领域的专家.姑且不辩论这句话是否正确,让我们到达10000小时的时候再回头来看吧. Hour 30 上回终点 Model 这里有一些java bean 的 风格约 ...

  3. Java Hour 15 以写小说的心态

    有句名言,叫做10000小时成为某一个领域的专家.姑且不辩论这句话是否正确,让我们到达10000小时的时候再回头来看吧. 突然想到我最近一直在追的小说,作者每天都会更新两章,而且质量挺高.所以从这篇开 ...

  4. PHP 文件上传类

    FileUpload.;                $];                $_newname = date(,). :                             To ...

  5. SAE上传web应用(包括使用数据库)教程详解及问题解惑

    转自:http://blog.csdn.net/baiyuliang2013/article/details/24725995 SAE上传web应用(包括使用数据库)教程详解及问题解惑: 最近由于工作 ...

  6. Android ImageView图片自适应 (转)

    网络上下载下来的图片自适应:android:adjustViewBounds="true"(其详细解释在下面)<ImageView     android:id=" ...

  7. java 线程的几种状态

    java thread的运行周期中, 有几种状态, 在 java.lang.Thread.State 中有详细定义和说明: NEW 状态是指线程刚创建, 尚未启动 RUNNABLE 状态是线程正在正常 ...

  8. JVM的GC实现详解

    新生代中的98%对象都是“朝生夕死”的,所以并不需要按照1:1的比例来划分内存空间,而是将内存分为一块比较大的Eden空间和两块较小的Survivor空间,每次使用Eden和其中一块Survivor. ...

  9. js:数据结构笔记12--排序算法(2)

    高级排序算法:(处理大数据:百万以上) 希尔排序:是插入排序的优化版: 首先设置间隔数组,然后按照每个间隔,分别进行排序: 如第一个间隔为5,首先a[5]与a[0]进行插入排序;然后a[6]和a[0] ...

  10. partial(C# 参考)

    分部类型定义允许将类.结构或接口的定义拆分到多个文件中. 在 File1.cs 中:     namespace PC { partial class A { } } 在 File2.cs 中:   ...