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

USACO 2006 November Gold

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

遗憾的是,有些土地相当贫瘠,不能用来种草。并且,奶牛们喜欢独占一块草地的感觉,于是John不会选择两块相邻的土地,也就是说,没有哪两块草地有公共边。

John想知道,如果不考虑草地的总块数,那么,一共有多少种种植方案可供他选择?(当然,把新牧场完全荒废也是一种方案)

。。。。。。。。。。。

此题看数据就知道是状压dp,f数组是记录每一行的状态是否存在贫瘠土地,mapp数组记录每一种状态是否有相邻的牛。

状态转移方程:dp[i][j]+=dp[i-1][k]; if(j&k==0)。

k是枚举出的上一行状态,j&k是保证两行没有相邻的牛。

#include<iostream>
#include<cstdio>
using namespace std;
const int N = 12;
const int mod = 100000000;
int dp[N+5][1<<N],m,n,a[N+5][N+5],ans,f[N+5];
bool mapp[1<<N];
int main() {
scanf("%d%d",&n,&m);
for(register int i=1; i<=n; i++)
for(register int j=1; j<=m; j++) {
scanf("%d",&a[i][j]);
f[i]=(f[i]<<1)+a[i][j];
}
for(register int i=0; i<1<<m; i++) {
bool flag=1;
int cnt=2;
for(register int j=0; j<m; j++) {
if(i>>j&1) {
if(cnt==1) {
flag=0;
break;
} else
cnt=1;
} else
cnt++;
}
if(flag) mapp[i]=1;
// cout<<i<<" "<<mapp[i]<<endl;
}
dp[0][0]=1;
for(register int i=1; i<=n; i++)
for(register int j=0; j<1<<m; j++) {
if(mapp[j] && ((f[i]&j)==j)) {
for(register int k=0; k<1<<m; k++)
if((j&k)==0){
dp[i][j]+=dp[i-1][k];
dp[i][j]%=mod;
}
}
}
for(register int i=0; i<1<<m; i++)
ans+=dp[n][i],ans%=mod;
ans%=mod;
printf("%d",ans);
}

poj 3254 Corn Field的更多相关文章

  1. 状压DP POJ 3254 Corn Fields

    题目传送门 /* 状态压缩DP:先处理硬性条件即不能种植的,然后处理左右不相邻的, 接着就是相邻两行查询所有可行的种数并累加 写错一个地方差错N久:) 详细解释:http://www.tuicool. ...

  2. poj - 3254 - Corn Fields (状态压缩)

    poj - 3254 - Corn Fields (状态压缩)超详细 参考了 @外出散步 的博客,在此基础上增加了说明 题意: 农夫有一块地,被划分为m行n列大小相等的格子,其中一些格子是可以放牧的( ...

  3. poj 3254 Corn Fields

    http://poj.org/problem?id=3254 Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissio ...

  4. POJ 3254 Corn Fields(状压DP)

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

  5. poj 3254 Corn Fields 国家压缩dp

    意甲冠军: 要在m行n陆行,有一些格您可以种树,别人做不到的.不相邻的树,我问了一些不同的共同拥有的法律. 分析: 从后往前种,子问题向父问题扩展,当种到某一格时仅仅有他和他后面的n-1个格子的情况对 ...

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

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

  7. POJ 3254 Corn Fields:网格密铺类 状压dp

    题目链接:http://poj.org/problem?id=3254 题意: 给你一片n*m的耕地,你可以在上面种玉米.但是其中有一些地方是荒芜的,不能种植.并且种植玉米的地方不能相邻.问你在这片地 ...

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

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

  9. POJ 3254 Corn Fields (状压dp)

    题目链接:http://poj.org/problem?id=3254 给你n*m的菜地,其中1是可以种菜的,而菜与菜之间不能相邻.问有多少种情况. 状压dp入门题,将可以种菜的状态用一个数的二进制表 ...

随机推荐

  1. oracle查询重复数据出现次数

    话不多数上代码: 我在Oracle数据库查数据,发现重复数据,于是我想把重复条数以及具体数据查出来: 下面是数据 然后我需要知道重复多少条 (重复十条,也就是有五条数据相同) SQL: select ...

  2. 72.Properties(配置文件)

    Properties(配置文件):主要用于存储配置文件到硬盘上面和读取配置文件 public class Properties extends Hashtable<Object,Object&g ...

  3. Postman Interceptor和postman更改id仍然无法使用的,从这里下载相同版本的postman和interceptor插件

    1.postman安装: chrome://extensions/打开,把下载好的postman插件拖到里面就可以了. 2.Postman interceptor安装: chrome://extens ...

  4. 设置ll命令

    ll 是 ls -l的别名,之所以 ll出现错误是因为没有定义别名. 如果要实现ll 命令,可以做如下操作: 1.编辑 ~./bashrc 添加 ls -l 的别名为 ll即可. vi /root/. ...

  5. php随机生成数字加字母的字符串

    function getRandomString($len, $chars=null) { if (is_null($chars)) { $chars = "ABCDEFGHIJKLMNOP ...

  6. 学习android文档 -- Adding the Action Bar

    1. Setting Up the Action Bar:users-sdk version 11以上可以使用holo主题:如果不使用holo主题,或者sdk版本较低,则需要在manifest文件的& ...

  7. 天照(amaterasu)

    天照(amaterasu) 有些时候,出题人真的不想写背景. 总而言之,天照现在有一个长度为 $ N $ 序列,她有 $ M $ 次询问,对于第 $ i $ 次询问 $ l_i,r_i,x_i $ 你 ...

  8. Spring源码剖析2:初探Spring IOC核心流程

    本文转载自互联网,侵删 本系列文章将整理到我在GitHub上的<Java面试指南>仓库,更多精彩内容请到我的仓库里查看 https://github.com/h2pl/Java-Tutor ...

  9. SQLServer如何手动设置id值(主键)的自动增长

    近期做东西,用到了对SQLServer数据库的操作.好吧,确实好久没看了,对这个数据库陌生到了极点,连最简单的如何设置一个id主键,让它随着插入数据的增多,自动增长id值的设置都忘记了,网上查了一下, ...

  10. MySql命令行无法显示中文

    好烦遇到了,遇到MySql命令行无法显示中文问题????? show variables like 'char%';//显示字符集 set names utf8;//设置字符集 describer t ...