orn Fields
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 17989   Accepted: 9474

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.

题目大意:标1的地能够放牛,0的不能放牛,放的牛不能相邻,问有多少种方案.
分析:状压dp经典题.
   令f[i][j]表示前i行中,第i行的状态为j的方案数,枚举第i-1行的状态k,如果k,j合法,那么f[i][j] += f[i - 1][k].先要预处理出第一行合法的状态.
   小优化:可以将每一行中合法的状态提出来.
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; const int mod = ;
int n,m,a[][],f[][ << ],cnt[],maxn,ans,sta[ << ],tot; bool check(int x,int y)
{
if (x & y)
return false;
return true;
} int main()
{
scanf("%d%d",&n,&m);
maxn = ( << m) - ;
for (int i = ; i <= maxn; i++)
{
if (i & (i << ))
continue;
sta[++tot] = i;
}
for (int i = ; i <= n; i++)
for (int j = ; j <= m; j++)
{
scanf("%d",&a[i][j]);
if (a[i][j] == )
cnt[i] |= ( << (m - j));
}
for (int i = ; i <= tot; i++)
if (check(cnt[],sta[i]))
f[][sta[i]] = ;
for (int i = ; i <= n; i++)
for (int j = ; j <= tot; j++)
{
if (!check(cnt[i - ],sta[j]))
continue;
for (int k = ; k <= tot; k++)
{
if (!check(cnt[i],sta[k]))
continue;
if (sta[k] & sta[j])
continue;
f[i][sta[k]] += f[i - ][sta[j]];
f[i][sta[k]] %= mod;
}
}
for (int i = ; i <= tot; i++)
{
ans += f[n][sta[i]];
ans %= mod;
}
printf("%d\n",ans); return ;
}

poj3254 Corn Fields的更多相关文章

  1. poj3254 Corn Fields 利用状态压缩求方案数;

    Corn Fields 2015-11-25 13:42:33 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10658   ...

  2. poj3254 Corn Fields (状压DP)

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

  3. POJ3254 Corn Fields(状压DP)

    题目给个n×m的地图,1可以放玉米0不可以,现在要放玉米,玉米上下左右不能相邻,问放法有几种. 当前一行的决策只会影响下一行,所以状压DP之: dp[i][S]表示前i行放完且第i行放玉米的列的集合是 ...

  4. POJ 3254 poj3254 Corn Fields

    题意:给出一个n行m列的草地,1表示肥沃,0表示贫瘠,现在要把一些牛放在肥沃的草地上,但是要求所有牛不能相邻,问你有多少种放法. 思路: DP[i][j]=sum(dp[i-1][k]); i表示当前 ...

  5. POJ3254 - Corn Fields(状态压缩DP)

    题目大意 给定一个N*M大小的土地,土地有肥沃和贫瘠之分(每个单位土地用0,1来表示贫瘠和肥沃),要求你在肥沃的单位土地上种玉米,如果在某个单位土地上种了玉米,那么与它相邻的四个单位土地是不允许种玉米 ...

  6. 【状压DP】poj3254 Corn Fields

    题意: 一块n*m的田,1表示这个地方可以种植,0代表这个地方不能种植.植物种植还必须满足两株植物不能相邻(横竖都不行).问共有几种种植方法,而且当什么都不种时认为是一种方法. 解题思路: 种植用1表 ...

  7. 【原创】【状态压缩DP】POJ3254 Corn Fields【新手向】

    一开始根本不会状压dp,上网各种找题解,但发现他们写的都很......反正我作为一个没有接触过状态压缩的,根本看不懂! 然后看了好多状态压缩的题的题解,总结了一下思路,思路很重要,有了思路转换成计算机 ...

  8. 【状压基础题】poj3254 Corn Fields

    题目大意 :农夫约翰有n*m块地,其中一些地荒掉了.玉米是一种傲娇的植物,种在相邻的地里会导致不孕不育.求所有种法数对100000000求余. 读入:第一行一个n一个m, 接下来是一个n行m列的矩形, ...

  9. Corn Fields——POJ3254状态压缩Dp

    Corn Fields Time Limit: 2000MS Memory Limit: 65536K Description Farmer John has purchased a lush new ...

随机推荐

  1. 随笔1-本想吐槽cnblog编辑器,但是今天猛然发现它竟然这么干净简洁

    日常用写作编辑器 多少和我一样有选择综合征的? 不愿意写博客的其中一个非常重要的原因是编辑器不好用! 博客的迁移是相当麻烦的! 所以定下cnblog写东西也是一种勇气!鼓励.... FlashNote ...

  2. NO.06--聊一聊“币”吧!

    近期博主更新的频率明显慢来 ,一来是最近的工作比较忙碌,几个项目几乎同时要上线.二来是在思考是不是把我平时生活中的一些事情写进来博客,不只是分享分享技术. 趁着区块链.比特币火爆,博主也算是略有涉猎, ...

  3. Vue2 v-bind:href 中如何使用过滤器

    <a class="topic_title" v-bind:href="info.id|getTitleHref" v-bind:title=" ...

  4. 智慧树mooc自动刷课代码

    最近学习javaScript和JQuery,恰好还有一门mooc没有看.结合学习的知识和其他人的代码:撸了一个自动播放课程的代码,同时自动跳过单章的测试题. 用电脑挂着不动就完事了. 如下: var ...

  5. leetcode28_C++实现strStr()函数

    实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始).如果不存在,则返 ...

  6. win7重装系统后设置Python2.7环境

    起因 台式机的主板莫名出现问题,显示器画面卡顿不能动,鼠标键盘无反应,在这种情况下只好按住电源键断电.下面重启后,显示器无画面,猜测开机后没有进BIOS.然后就拆机箱,拔下电源线后撬起主板电池几秒再放 ...

  7. 机器学习之决策树(ID3)算法

    最近刚把<机器学习实战>中的决策树过了一遍,接下来通过书中的实例,来温习决策树构造算法中的ID3算法. 海洋生物数据:   不浮出水面是否可以生存 是否有脚蹼 属于鱼类 1 是 是 是 2 ...

  8. 微软职位内部推荐-Senior Software Engineer II-Search

    微软近期Open的职位: Do you want to work on a fast-cycle, high visibility, hardcore search team with ambitio ...

  9. content-length与Transfer-Encoding: chunked的问题释疑

    http返回头中content-length与Transfer-Encoding: chunked的问题释疑 先说说问题出现的背景: 公司服务器与手机客户端交互,客户端请求一个动态生成的XML文件,在 ...

  10. nagios监控安装esxi的服务器(宿主机)

    首先,该博文大部分内容来自网络,少部分是自己监控过程中遇到的问题.如果有侵权,请联系告知!!! 现在互联网公司,有能力的都是自己研发监控系统,要么就是zabbix或者小米的监控,还都二次开发等等,可能 ...