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是保证两行没有相邻的牛。

  1. #include<iostream>
  2. #include<cstdio>
  3. using namespace std;
  4. const int N = 12;
  5. const int mod = 100000000;
  6. int dp[N+5][1<<N],m,n,a[N+5][N+5],ans,f[N+5];
  7. bool mapp[1<<N];
  8. int main() {
  9. scanf("%d%d",&n,&m);
  10. for(register int i=1; i<=n; i++)
  11. for(register int j=1; j<=m; j++) {
  12. scanf("%d",&a[i][j]);
  13. f[i]=(f[i]<<1)+a[i][j];
  14. }
  15. for(register int i=0; i<1<<m; i++) {
  16. bool flag=1;
  17. int cnt=2;
  18. for(register int j=0; j<m; j++) {
  19. if(i>>j&1) {
  20. if(cnt==1) {
  21. flag=0;
  22. break;
  23. } else
  24. cnt=1;
  25. } else
  26. cnt++;
  27. }
  28. if(flag) mapp[i]=1;
  29. // cout<<i<<" "<<mapp[i]<<endl;
  30. }
  31. dp[0][0]=1;
  32. for(register int i=1; i<=n; i++)
  33. for(register int j=0; j<1<<m; j++) {
  34. if(mapp[j] && ((f[i]&j)==j)) {
  35. for(register int k=0; k<1<<m; k++)
  36. if((j&k)==0){
  37. dp[i][j]+=dp[i-1][k];
  38. dp[i][j]%=mod;
  39. }
  40. }
  41. }
  42. for(register int i=0; i<1<<m; i++)
  43. ans+=dp[n][i],ans%=mod;
  44. ans%=mod;
  45. printf("%d",ans);
  46. }

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. 分布式项目web.xml配置文件的表头

    <?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://w ...

  2. 【dart学习】-- Dart之类和对象

    一,概述 类(Class)是面向对象程序设计,实现信息封装的基础.类是一种用户定义的类型.每个类包含数据说明和一组操作数据或传递消息的函数.类的实例称为对象. Dart的类与其它语言都有很大的区别,比 ...

  3. STM32嵌入式开发学习笔记(二):将功能封装为库文件

    将所有的函数都堆在main.c文件里不是好的选择,庞大的代码文件会是你维护的障碍,明智的做法是,一种功能封装到一个库文件里. 库文件就是你代码开始部分写的#include<xxxx.h>里 ...

  4. java求两个数中的大数

    java求两个数中的大数 java中的max函数在Math中 应用如下: int a=34: int b=45: int ans=Math.max(34,45); 那么ans的值就是45.

  5. WIN7下怎么安装iis教程

    点击开始→控制面板,然后再点击程序,勿点击卸载程序,否则到不了目标系统界面. 2 然后在程序和功能下面,点击打开和关闭windows功能. 3 进入Windows功能窗口,然后看到internet信息 ...

  6. jQuery 对文档的操作

    通过jquery方式实现页面各种节点的追加.修改.删除.复制等操作 节点追加 1 父子关系追加 /*************************************************** ...

  7. Java全栈,MySQL搞透,架构手到擒来,还有面试官搞不定?

    五月最后一天啦,时间过得真快,做技术的难免做了几年就感觉很迷茫,那就需要多读点书,多学点技术才能有安全感. 栈长之前推荐过不少极客时间的课程,几乎每周都推荐一个,很多朋友评论说,课程太多学不过来,今天 ...

  8. Win10下安装erl和RabbitMQ踩坑【版本不兼容】

    版本不兼容 erl:otp_win64_21.0.1.exe rabbitmq:rabbitmq-server-3.8.1.exe(2019.12.06时最新版) 根据官方文档的匹配表:https:/ ...

  9. P3224 [HNOI2012]永无乡(平衡树合并)

    题目描述 永无乡包含 nn 座岛,编号从 11 到 nn ,每座岛都有自己的独一无二的重要度,按照重要度可以将这 nn 座岛排名,名次用 11 到 nn 来表示.某些岛之间由巨大的桥连接,通过桥可以从 ...

  10. Java-向上转型后调用方法和属性的注意事项

    /*多态的注意事项 (1)如果子类重写了父类方法, 意味着子类里定义的方法彻底覆盖了父类里同名的方法, 系统将不可能把父类里的方法转移到子类中 补充对(1)的个人理解: 当s向上转型传给了b, b即使 ...