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. Spring MVC exception - Invoking request method resulted in exception : public static native long java.lang.System.currentTimeMillis()

    最近在线上系统发现下面的异常信息: 2014-10-11 11:14:09 ERROR [org.springframework.web.servlet.mvc.annotation.Annotati ...

  2. hihocoder 1356 分隔相同整数

    时间限制:10000ms单点时限:1000ms内存限制:256MB 描述 给定一个包含N个整数的数组A.你的任务是将A重新排列,使得任意两个相等的整数在数组中都不相邻. 如果存在多个重排后的数组满足条 ...

  3. java 和 mysql 获取周 星期 的第一天 最后一天 或者 月的 日期(字符串转日期,日期转字符串,日期加减)

    获取周的第一天,最后一天 System.out.println(getStartEndDate("2016-05-01", 1)); 获取星期的第一天和最后一天 System.ou ...

  4. 《JavaScript权威指南》学习笔记 第一天。

    这是零零散散的笔记,作为自己看书打demo的笔记.不足为各位学习,留作自己复习知识点备用. 1.检测对象中某个属性存在不存在: <script> // in 运算符 //不管是对象的自有属 ...

  5. CDN网络(二)之配置和优化CDN核心缓存软件--squid

    前言 squid是众多CDN厂商使用的核心缓存软件,都在已有的基础上进行二次开发.在部署squid的时候,建议遵循下面的规范. 1. 使用大内存服务器 对于热点文件,我们让squid用内存缓存,这样大 ...

  6. 通过rsync搭建一个远程备份系统(一)

    前言 我公司是电子商务公司,全部是linux系统,每天的网站数都在增加,为了保证安全,需要建立一个远程容灾系统,将网站数据每天凌晨1点备份到远程服务器上,由于数据量大,每天进行进行增量备份,仅仅备份当 ...

  7. 面向服务架构(SOA)和企业服务总线(ESB)

    http://www.cnblogs.com/shanyou/archive/2008/04/19/1161452.html 学习和研究在企业中实施面向服务架构(SOA),简单回顾SOA和ESB,重点 ...

  8. 9.25 DOM作业

    一<style type="text/css">*{margin:0px auto; padding:0px; font-family:微软雅黑; font-size: ...

  9. 谁都能看懂的单点登录(SSO)实现方式(附源码)

    SSO的基本概念 SSO英文全称Single Sign On(单点登录).SSO是在多个应用系统中,用户只需要登录一次就可以访问所有相互信任的应用系统.它包括可以将这次主要的登录映射到其他应用中用于同 ...

  10. cocos2d 2.2.6 win7下的配置

    我搭建cocos2.6的开发环境需要安装工具包括: 1.Visual Studio 2012(由于不兼容win7,需要安装Update 4)和虚拟光驱daemon tool,虚拟光驱的下载地址:htt ...