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. js将map转换成数组

    /** * map转数组. * * @param {Map}map * map对象 * @return 数组 */ Share.map2Ary = function(map) { var list = ...

  2. Java与数据库类型对照表

     数据库类型  Java类型  INTEGER  int or java.lang.Integer  BIGINT  long or java.lang.Long  SMALLINT  short o ...

  3. 告别div,可以代替div的几个标签

    几个最常用的用来代替DIV的HTML5元素 虽说html5中大多数功能性的元素如<video><canvas><audio>等还得不到当前主流浏览器的支持(主要就是 ...

  4. 禁用SettingSyncHost.exe

    TASKKILL /F /IM SettingSyncHost.exe /T

  5. Replace Nested Conditional with Guard Clauses(用卫语句代替嵌套循环)

    函数中的条件逻辑,使人难以看清正常的执行路径. 使用卫语句表现所有特殊情况. double getPayAmount() {double result;if (_isDead) result = de ...

  6. Struts文件上传allowedTypes问题,烦人的“允许上传的文件类型”

    Struts的文件上传问题,相信很多人都会使用allowedTypes参数来配置允许上传的文件类型,如下. <param name="allowedTypes"> im ...

  7. 关于printf函数输出先后顺序的讲解!!

    对于printf函数printf("%d%d\n",a,b);函数的实际输出顺序是这样的先计算出b,然后在计算a,接着输出a,最后在输出b:例子如下:#include<ios ...

  8. Android数据存储之sharedpreferences与Content Provider

    android中对数据操作包含有: file, sqlite3, Preferences, ContectResolver与ContentProvider前三种数据操作方式都只是针对本应用内数据,程序 ...

  9. KVM中Linux虚拟机的硬盘添加方法

    [root@cache01 ~]# df -hT Filesystem Type Size Used Avail Use% Mounted on /dev/mapper/VolGroup-lv_roo ...

  10. Java Hour3

    有句名言,叫做10000小时成为某一个领域的专家.姑且不辩论这句话是否正确,让我们到达10000小时的时候再回头来看吧. 本文作者Java 现经验约为2 Hour,请各位不吝赐教. Hour3 包和i ...