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. iOS开发之多线程技术—GCD篇

    本篇将从四个方面对iOS开发中GCD的使用进行详尽的讲解: 一.什么是GCD 二.我们为什么要用GCD技术 三.在实际开发中如何使用GCD更好的实现我们的需求 一.Synchronous & ...

  2. 基于zookeeper+mesos+marathon的docker集群管理平台

    参考文档: mesos:http://mesos.apache.org/ mesosphere社区版:https://github.com/mesosphere/open-docs mesospher ...

  3. Python异常(基础) except

    为什么要异常处理机制:在程序调用层数较深时,向主调函数传递错误信息需要层层return 返回比较麻烦,用异常处理机制可以较简单的传送错误信息 什么是错误 错误是指由于逻辑或语法等导致一个程序已无法正常 ...

  4. 阿里IPO法律咨询费达1580万美元 为Facebook六倍

    据路透社报道,阿里巴巴集团周五在 IPO (首次公开招股)更新文件中披露,将向美国盛信律师事务所(Simpson Thacher)以及其他为 IPO 提供咨询服务的律师事务所支付 1580 万美元的法 ...

  5. ExpressJS基础概念及简单Server架设

    NodeJS Node.js 是一个基于 Chrome V8 引擎的 JavaScript 运行环境.Node.js 使用了一个事件驱动.非阻塞式 I/O 的模型,使其轻量又高效.Node.js 的包 ...

  6. python基础-02-while格式化逻辑运算

    python其他知识目录 1.循环打印“我是小马过河” while True:    print('我是小马过河') #4.用while从一打印到10 #5.请通过循环,1 2 3 4 5 6 8 9 ...

  7. 《C》数据类型

  8. QT中文乱码解决方法

    由于我毕设的界面是用Qt做的,之前没怎么接触过Qt,所以实现过程中遇到不少小问题,头一个就是这个. 现如今宝宝将其记录下来,供同样有需要的同学或者自己以后方便查阅. 1.所有文件编码格式须一致 不统一 ...

  9. Hexo博客搭建全解

    [原创,转载请附网址:http://dongshuyan.top] 欢迎来到莫与的博客,第一篇记录了一下怎么写一篇博客,以方便之后写博客~ #从配置说起下载安装Git与Node.js略过 1.安装he ...

  10. 第七次java作业

     interface Pet{public String getName();public String getColor();public int getAge();}class Cat imple ...