题意:

有一块n*m的土地

0代表不肥沃不可以放牛 1代表肥沃可以放牛

且相邻的草地不能同时放牛

问最多有多少种放牛的方法并对1e8取模

思路:

典型的状压dp 能状态压缩 能状态转移

能状态压缩的题的特点就是只有两种状态

所以用0 1表示两种状态 用位运算判断是否符合条件

然后将前一行的合理状态转移到后一行

最后统计最后一行的状态

dp[i][j]代表第i行以第j种状态放牛时有多少种不同的状态

(c++的语言特性是 封装 继承 多态~)

 /* ***********************************************
Author :Sun Yuefeng
Created Time :2016/10/31 17:59:53
File Name :C.cpp
************************************************ */ #include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<bitset>
#include<map>
#include<set>
#include<stack>
#include<vector>
#include<queue>
#include<list>
#define M(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
const int maxn=<<;
const int mod=1e8;
int dx[]= {,,,-,,-,,-};
int dy[]= {,-,,,-,,,-}; int _map[maxn]; //记录给出的每块地的状态
int st[maxn]; //记录每一行的状态
int dp[][maxn]; bool judge(int x) //判断相邻两位是否同时为1
{
return (x&(x<<));
} bool judge(int x,int y) //判断两位是否同时为1
{
return _map[x]&st[y];
} int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int n,m;
while(cin>>n>>m){
int x;
M(dp,),M(_map,),M(st,);
for(int i=;i<n;i++)
{
for(int j=;j<m;j++)
{
cin>>x;
if(!x)
{
_map[i]+=(<<j); //向图中添加空地
}
}
}
int k=;
for(int i=;i<(<<m);i++) //初始化能不能以i状态放牛
{
if(!judge(i))
{
st[k++]=i;
}
}
for(int i=;i<k;i++) //初始化第一行状态
{
dp[][i]=(!judge(,i));
}
for(int i=;i<n;i++)
{
for(int j=;j<k;j++)
{
if(judge(i,j)) continue; //判断第i行是否能以j的方式放牛
for(int l=;l<k;l++)
{
if(judge(i-,l)) continue; //判断上一行状态
if(!(st[j]&st[l])) //符合题意转移状态
dp[i][j]+=dp[i-][l];
}
}
}
int ans=;
for(int i=;i<k;i++)
{
ans+=dp[n-][i];
ans%=mod;
}
cout<<ans<<endl;
}
return ;
}

[ An Ac a Day ^_^ ] 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. Poj - 3254 Corn Fields (状压DP)(入门)

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

  4. poj 3254 Corn Fields 状压dp入门

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

  5. POJ 1684 Corn Fields(状压dp)

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

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

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

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

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

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

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

  9. POJ 3254. Corn Fields 状态压缩DP (入门级)

    Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9806   Accepted: 5185 Descr ...

随机推荐

  1. jQuery extend函数详解

    一 jQuery的扩展方法原型是 $.extend(dest,src1,src2,src3); 含义是将src1,src2,src3合并到dest中,返回值为合并后的dest,该方法合并后,dest的 ...

  2. window.open()详解及浏览器兼容性问题

    一.基本语法:window.open(pageURL,name,parameters)其中:pageURL 为子窗口路径name  为子窗口名字parameters 为窗口参数(各参数用逗号分隔) 二 ...

  3. MyEclipse从数据库表反向生成实体类之Hibernate方式(反向工程)

    一.打开Myeclipse Database Explorer 二.新建[New..] 三.选择数据库,添加驱动 四.测试连接 五.选中数据库中的表单击右键选择[Hibernate Rever..] ...

  4. json-smart 使用示例

    json-smart 使用示例 json是一种通用的数据格式.相比与protocal buffer.thrift等数据格式,json具有可读性强(文本).天生具备良好的扩展性(随意增减字段)等优良特点 ...

  5. jQuery的MP3、视频播放器jPlayer

    jplayer是一个纯代码的html5音乐.视频播放器. 支持单个.多个音乐或视频播放,支持各种主流的媒体文件. 使用方法: 1.提供json参数 [ { "title": &qu ...

  6. WPF4.5新特性(MSDN的翻译读不太懂)

    WPF4.5新特性(MSDN的翻译读不太懂) 1. 新的Doctype声明 XHTML的声明太长了,我相信很少会有前端开发人员能手写出这个Doctype声明. <!DOCTYPE html PU ...

  7. 使用Struts2实现文件的上传和下载

    (一)单个文件的上传步骤: 1.拷贝jar包:commons-fileupload.jar,  commons-io.jar 下载链接(文件上传.rar):http://www.cnblogs.com ...

  8. hdu4474 Yet Another Multiple Problem

    Yet Another Multiple Problem Description There are tons of problems about integer multiples. Despite ...

  9. android.database.CursorIndexOutOfBoundsException: Index <m> requested, with a size of <n>

    遇到这样的问题比较郁闷,造成上述问题的原因也是多种多样的. 总结一下原因: 1:得到cursor对象,没有moveToPosition()或者moveToNext()等游标操作就进行cursor.ge ...

  10. Android Apk获取包名和Activity名称

    一.使用aapt(Android Asset Packaging Tool)工具获取: 1.配置Android环境: a.添加build-tools/android路径到系统环境变量的中Path中,注 ...