Corn Fields
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 4739   Accepted: 2506

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.

 

题意:农夫FJ有一块n行m列的矩形土地, 有的土地是肥沃的,可以在这些土地上放牛(用1表示),有的土地不能放牛(用0表示),而且相邻的可以放牛的格子不能同时有牛,问FJ一共有多少种放牛的方案(一头牛都不放也是一种方案)。

分析:以样例为例,我们可以一行一行的考虑。假如对于每一行,用1表示放牛,0表示不放牛,

第一行的状态为:000  001 010  011(舍弃) 100 101 110(舍弃) 111(舍弃)

符合题意的状态只有5个,所以第一行有5种方案。

第二行的状态为:000 010

但是第二行中的010和第一行中的010冲突,所以如果第二行状态为010时,共有4种方案;状态为000时,有5种方案,所以一共有4+5=9种方案。

分析完我们会发现,对于每一行,都可以一个01串来表示这一行的状态,而这个状态可以用一个十进制整数来代替,也就是说,把这个状态压缩成了一个十进制整数,所以称为是状态压缩。

本题中,dp[i][j] 就表示第i行状态为j时的方案数。

状态压缩dp中最常见的就是位运算,因为位运算可以很方便的判断当前状态是否合法。例如本题中判断第i行是不是有两块相邻的土地同时都有牛,假设当前状态为X,那么只需要判断X&(X>>1)或者X&(X<<1)的结果是不是0,如果是0,说明没有相邻的,否则就说明有相邻的。

 
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<algorithm>
#include<cmath>
#define M(a,b) memset(a,b,sizeof(a))
#define INF 0x3f3f3f3f using namespace std; int num[][];
int dp[][]; const int mod = ; int main()
{
int N,M;
while(scanf("%d%d",&M,&N)==)
{
for(int i = ;i<M;i++)
for(int j = ;j<N;j++)
{
scanf("%d",&num[i][j]);
}
M(dp,);
for(int i = ;i<(<<N);i++)
{
int flag = ;
for(int j = ;j<N;j++)
{
if(i&(<<j)&&num[][j]==||(i&(<<j+)&&(i&(<<j))))
{flag = ; }
}
if(flag) {dp[][i] = ; //cout<<i<<endl;
}
}
for(int x = ;x<M;x++)
{
for(int i = ;i<(<<N);i++)
{
int flag = ;
for(int j = ; j<N;j++)
{
if(i&(<<j)&&num[x][j]==||(i&(<<j+)&&(i&(<<j))))
{flag = ; }
}
if(flag)
{
for(int j = ;j<(<<N);j++)
{
int fg = ;
for(int k = ; k<N; k++)
{
if((i&(<<k))&&(j&(<<k)))
{fg = ; }
}
if(fg)
{dp[x][i]= (dp[x][i]+dp[x-][j])%mod;}
}
}
}
}
int ans = ;
for(int i = ;i<(<<N);i++)
{
ans = (ans+dp[M-][i])%mod;
}
printf("%d\n",ans);
}
return ;
}
 

POJ 3254 Corn Fields(状态压缩DP)的更多相关文章

  1. POJ 3254 Corn Fields (状态压缩DP)

    题意:在由方格组成的矩形里面种草,相邻方格不能都种草,有障碍的地方不能种草,问有多少种种草方案(不种也算一种方案). 分析:方格边长范围只有12,用状态压缩dp好解决. 预处理:每一行的障碍用一个状态 ...

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

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

  3. POJ 3254 Corn Fields 状态压缩DP (C++/Java)

    id=3254">http://poj.org/problem? id=3254 题目大意: 一个农民有n行m列的地方,每一个格子用1代表能够种草地,而0不能够.放牛仅仅能在有草地的. ...

  4. poj - 3254 Corn Fields (状态压缩dp入门)

    http://poj.org/problem?id=3254 参考:http://blog.csdn.net/accry/article/details/6607703 农夫想在m*n的土地上种玉米, ...

  5. POJ 3254 Corn Fields状态压缩DP

    下面有别人的题解报告,并且不止这一个状态压缩题的哦···· http://blog.csdn.net/accry/article/details/6607703 下面是我的代码,代码很挫,绝对有很大的 ...

  6. [ACM] POJ 3254 Corn Fields(状态压缩)

    Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8062   Accepted: 4295 Descr ...

  7. poj 3254 Corn Fields 国家压缩dp

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

  8. POJ 3254 Corn Fields 状态压缩

    这题对我真的非常难.实在做不出来,就去百度了,搜到了一种状压DP的方法.这是第一种 详细见凝视 #include <cstdio> #include <cstring> #in ...

  9. poj 3254 Corn Fields_状态压缩dp

    感谢:http://www.cnblogs.com/ka200812/archive/2011/08/11/2135607.html 让我搞懂了. #include <iostream> ...

随机推荐

  1. C语言实现贪吃蛇源码

    先放效果 源代码 //2016-2-12 //zhaoyu //Gmail:zhaoyu1995.com@gmail.com //Language: C //Platform:Code::Blocks ...

  2. python dict.get()和dict['key']的区别

    先看代码: In [1]: a = {'name': 'wang'} In [2]: a.get('age') In [3]: a['age'] --------------------------- ...

  3. spring+hibernate ---laobai

    biz包: package com.etc.biz; import java.util.List; import org.springframework.orm.hibernate3.support. ...

  4. UML活动图与流程图的区别

    http://blog.chinaunix.net/uid-11572501-id-3847592.html UML活动图与流程图的区别 (1).流程图着重描述处理过程,它的主要控制结构是顺序.分支和 ...

  5. 如何排查APP服务端和客户端是否支持ATS

    服务端排查 取得客户端直接连接的服务端域名及端口,例如mob.com.cn,端口443,即HTTPS默认端口.针对公网可访问的生产环境地址,建议使用的在线监测工具.https://wosign.ssl ...

  6. win 2012 关闭IE增强设置

  7. HTML学习笔记——选择器

    1> ID选择器.交叉选择器.群组选择器.子代选择器 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN& ...

  8. 安装weinre在PC端调试移动端

    1.使用node安装weinre. 2.启动weinre, weinre --httpPort 8081  --boundHost -all- 3.在浏览器打开 http://localhost:80 ...

  9. css3 中的transition和transform

    我以前始终都把他搞反,或者是混淆.现在可以稍微小结下. Transition:CSS3中处理动画的一个样式:只涉及动画起始和终止两个状态.如果涉及到一个动画的各个时间或者状态,那就必须要用到的另外一个 ...

  10. union联合体使用详解

    1.联合体联合体(union)与结构体(struct)有一些相似之处.但两者有本质上的不同.在结构体中,各成员有各自的内存空间, 一个结构变量的总长度是各成员长度之和.而在联合体中,各成员共享一段内存 ...