Corn Fields
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 17749   Accepted: 9342

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.

Source

题意:给一个n*m的矩阵,上面1表示肥沃的土地,0表示贫瘠的土地,然后要让牛必须站在肥沃的土地上,又不能相邻(可以整个都不站牛)

题解:那就用二进制来表示整个土地的样子,状态转移是dp[i][j] = sum(dp[i-1][k]) 表示第i行的站法是由i-1行所有可能的站法并且两行不能冲突的和

技巧:

1.判断一个数字是否有相邻的两个1 :x&(x<<1)=0,则表示没有,否则有。

2.两行是否有上下相邻的:若x&y=0,则没有,否则有。

3. 判断当前站位是否满足土地条件:若maps[i]=maps[i] | x,则x情况下的站位满足土地条件。

代码:

 //#include "bits/stdc++.h"
#include "cstdio"
#include "map"
#include "set"
#include "cmath"
#include "queue"
#include "vector"
#include "string"
#include "cstring"
#include "time.h"
#include "iostream"
#include "stdlib.h"
#include "algorithm"
#define db double
#define ll long long
//#define vec vector<ll>
#define Mt vector<vec>
#define ci(x) scanf("%d",&x)
#define cd(x) scanf("%lf",&x)
#define cl(x) scanf("%lld",&x)
#define pi(x) printf("%d\n",x)
#define pd(x) printf("%f\n",x)
#define pl(x) printf("%lld\n",x)
#define rep(i, x, y) for(int i=x;i<=y;i++)
const int N = 1e5 + ;
const int mod = 1e9 + ;
const int MOD = mod - ;
const db eps = 1e-;
const db PI = acos(-1.0);
using namespace std;
int maps[],ok[N];
int f[][N];
int n,m;
bool cal1(int i){
return i&(i<<);//检查相邻的1
}
bool cal2(int x,int y){
return maps[x]==(maps[x]|ok[y]);//检查合理的站位与土地条件是否冲突
}
int main()
{
while(scanf("%d%d",&n,&m)==&&n!=)
{
memset(maps,, sizeof(maps));
memset(f,, sizeof(f));
for(int i=;i<n;i++)
{
for(int j=;j<m;j++){
int x;
ci(x);
if(x!=) maps[i]+=(<<j);//更新第i行的土地
}
}
int cnt=;
for(int i=;i<(<<m);i++){
if(!cal1(i)) ok[cnt++]=i;//保存合理的站位
}
for(int i=;i<cnt;i++){
if(cal2(,i)) f[][i]=;//处理出第一行的合理站位
}
ll ans=;
for(int i=;i<n;i++){
for(int j=;j<cnt;j++){
if(!cal2(i,j)) continue;//检查对于第i行是否合理
for(int k=;k<cnt;k++){//检查与i-1行是否冲突
if(!(ok[j]&ok[k])) f[i][j]+=f[i-][k];
}
}
}
for(int i=;i<cnt;i++) ans+=f[n-][i],ans%=;
pl(ans);
}
return ;
}
 

POJ 3254 状压DP(基础题)的更多相关文章

  1. poj 3254 状压dp入门题

    1.poj 3254  Corn Fields    状态压缩dp入门题 2.总结:二进制实在巧妙,以前从来没想过可以这样用. 题意:n行m列,1表示肥沃,0表示贫瘠,把牛放在肥沃处,要求所有牛不能相 ...

  2. POJ 3254 (状压DP) Corn Fields

    基础的状压DP,因为是将状态压缩到一个整数中,所以会涉及到很多比较巧妙的位运算. 我们可以先把输入中每行的01压缩成一个整数. 判断一个状态是否有相邻1: 如果 x & (x << ...

  3. POJ 3254 状压DP

    题目大意: 一个农民有一片n行m列 的农场   n和m 范围[1,12]  对于每一块土地 ,1代表可以种地,0代表不能种. 因为农夫要种草喂牛,牛吃草不能挨着,所以农夫种菜的每一块都不能有公共边. ...

  4. Corn Fields(POJ 3254状压dp)

    题意: n*m网格1能放0不能放 放的格子不能相邻 求一共多少种可放的方案. 分析: dp[i][j]第i行可行状态j的的最大方案数,枚举当前行和前一行的所有状态转移就行了(不放牛也算一种情况) #i ...

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

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

  6. hdu 3254 (状压DP) Corn Fields

    poj 3254 n乘m的矩阵,1表示这块区域可以放牛,0,表示不能,而且不能在相邻的(包括上下相邻)两个区域放牛,问有多少种放牛的方法,全部不放也是一种方法. 对于每块可以放牛的区域,有放或者不放两 ...

  7. 【bzoj1087】【互不侵犯King】状压dp裸题(浅尝ACM-D)

    [pixiv] https://www.pixiv.net/member_illust.php?mode=medium&illust_id=54329606 向大(hei)佬(e)势力学(di ...

  8. 【bzoj3195】【 [Jxoi2012]奇怪的道路】另类压缩的状压dp好题

    (上不了p站我要死了) 啊啊,其实想清楚了还是挺简单的. Description 小宇从历史书上了解到一个古老的文明.这个文明在各个方面高度发达,交通方面也不例外.考古学家已经知道,这个文明在全盛时期 ...

  9. 7月15日考试 题解(链表+状压DP+思维题)

    前言:蒟蒻太弱了,全打的暴力QAQ. --------------------- T1 小Z的求和 题目大意:求$\sum\limits_{i=1}^n \sum\limits_{j=i}^n kth ...

随机推荐

  1. JavaScript new 操作符 OOP(一)

    什么是对象    对象是单个实物的抽象,通常需要一个模板,表示某一类实物的共同特征,然后对象根据这个模板生成. 对象是一个容器,封装了属性(property)和方法(method),属性是对象的状态, ...

  2. Python函数(3)

    一.装饰器 什么是装饰器,装饰器就是用于拓展原来函数功能的一种函数 装饰器就是用来为被装饰对象添加新功能的工具,装饰器本身可以是任意可调用对象,被装饰的对象也可以是任意可调用对象 装饰器遵循一个关键原 ...

  3. css颜色 hsla 和line-gradient

    h 表示色调 从0-360 s 饱和度  0 - 100% l 亮度    0 -100% a 透明度  0-1

  4. The fifth day

    All men cannot be first . 今日单词: first(形容词):第一的:基本的:最早的:(副词):第一:首先 翻译:不可能人人都是第一名. <Only Love>-- ...

  5. Dictionary and KeyValuePair.

    简单一句话: Dictionary 是 由 KeyValuePair结构 组成的集合 The Dictionary<TKey, TValue>.Enumerator.Current pro ...

  6. 添加并发请求PDF到工作流附件

    本节实现将并发请求输出PDF文件添加到工作流附件 省去了工作流中其他部分,只对附件部分介绍 1.       建立一个类型为Document的Attribute

  7. ASP.NET与json对象互转

    这两天写这个xml跟json的读写,心累啊,也不是很理解,请大家多指教 首先来个热身菜做一个简单的解析json 在script里写一个简单的弹窗效果 <script> //script里简 ...

  8. webpack前端构建工具学习总结(一)之webpack安装、创建项目

    npm是随nodeJs安装包一起安装的包管理工具,能解决NodeJS代码部署上的很多问题: 常见的使用场景有以下几种: 允许用户从NPM服务器下载别人编写的第三方包到本地使用. 允许用户从NPM服务器 ...

  9. 1.4 配置备份策略(Policy)

    1.1 配置备份策略(Policy) 一个备份策略由四部分组成. Attributes(属性) Policy是否Active Policy类型 由此Policy产生的任务的优先级 使用的Storage ...

  10. spring中使用i18n(国际化)

    简单了解i18n i18n(其来源是英文单词internationalization的首末字符i和n,18为中间的字符数)是“国际化”的简称.在资讯领域,国际化(i18n)指让产品(出版物,软件,硬件 ...