Corn Fields

Time Limit: 2000MS Memory Limit: 65536K

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

题意:在一个n*m的草场上,每一块草场都可以放一只牛,有的草场是贫瘠的,所以不能放牛。由于这些牛比较孤僻,所以不喜欢在自己吃草的地方周围有其他的牛(上下左右),问总共有多少种放法。

思路:在图上的每一个点都可能放牛(除去荒地),所以搜索的时间复杂度很高。

  • 假设我们知道矩阵的n和m,我们用二进制表示在一行的放牛的状态(不考虑荒地),则所有的状态为[0,1 << m),状态是否符合可以根据(x&(x<<1)),返回值为0表示没有相邻的1,则符合条件,否则不符合条件,这样我们就记录在草场宽为m时的所有可以放的情况,记录在Sta数组中。
  • 由于有荒地,所以对于每一行的第j个草地如果为荒地则为1,否则为零,这样就可以每一行有一个数表示他们的草地的状态,存在Map数组中,如果要判断在符合情况的方式中是不是有矛盾的,可以用Map[i]&Sta[j]==0(表示第i行草地与第j种方式),如果等于零则符合,不等于零则不符合(在纸上写写看看为什么)。
  • 首先将第一行的所以状态初始化即Dp[1][j]=(Map[1]&Sta[j])==0?1:0,然后根据第一行的所有状态去枚举第二行看是否符合,即
  1. for i=2 -> n
  2. {
  3. for j=0 -> num
  4. {
  5. if Map[i]&Sta[j] == 1
  6. {
  7. continue;
  8. }
  9. for k=0 -> num
  10. {
  11. if Map[i-1]&Sta[k] == 1
  12. {
  13. continue;
  14. }
  15. if Sta[j]&Sta[k]==0
  16. {
  17. Dp[i][j]+=Dp[i-1][k]
  18. }
  19. }
  20. }
  21. }

具体情况见代码

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <cstdlib>
  4. #include <iostream>
  5. #include <algorithm>
  6. using namespace std;
  7. const int Max = 1<<13;
  8. const int Mod = 100000000;
  9. int Dp[15][Max];
  10. int Map[15];
  11. int Sta[Max];
  12. bool Judge(int x)
  13. {
  14. return (x&(x<<1));
  15. }
  16. bool Dec(int x,int y)
  17. {
  18. return (Map[x]&Sta[y]);
  19. }
  20. int main()
  21. {
  22. int n,m;
  23. while(~scanf("%d %d",&n,&m))
  24. {
  25. memset(Map,0,sizeof(Map));
  26. memset(Dp,0,sizeof(Dp));
  27. memset(Sta,0,sizeof(Sta));
  28. int data;
  29. for(int i=1;i<=n;i++)
  30. {
  31. for(int j=1;j<=m;j++)
  32. {
  33. scanf("%d",&data);
  34. if(data==0)
  35. {
  36. Map[i]+=(1<<(j-1)); //每一行的情况用一个数来表示
  37. }
  38. }
  39. }
  40. int num = 0 ;
  41. for(int i=0;i<(1<<m);i++)//所有符合方式
  42. {
  43. if(!Judge(i))
  44. {
  45. Sta[num++] = i;
  46. }
  47. }
  48. for(int i=0;i<num;i++)//第一行的所有状态初始化
  49. {
  50. if(!Dec(1,i))
  51. {
  52. Dp[1][i]=1;
  53. }
  54. }
  55. for(int i=2;i<=n;i++)
  56. {
  57. for(int j=0;j<num;j++)
  58. {
  59. if(Dec(i,j))// 是否符合
  60. {
  61. continue;
  62. }
  63. for(int k=0;k<num;k++)
  64. {
  65. if(Dec(i-1,k))//i-1行是否符合第k种情况
  66. {
  67. continue;
  68. }
  69. if(!(Sta[j]&Sta[k])) //上下行之间也互相的满足条件
  70. {
  71. Dp[i][j]+=Dp[i-1][k];
  72. }
  73. }
  74. }
  75. }
  76. int ans =0;
  77. for(int i=0;i<num;i++)
  78. {
  79. ans+=Dp[n][i];
  80. ans%=Mod;
  81. }
  82. printf("%d\n",ans);
  83. }
  84. return 0;
  85. }

Corn Fields——POJ3254状态压缩Dp的更多相关文章

  1. Corn Fields poj3254(状态压缩DP)

    Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6081   Accepted: 3226 Descr ...

  2. 洛谷P1879 [USACO06NOV]玉米田Corn Fields (状态压缩DP)

    题目描述 Farmer John has purchased a lush new rectangular pasture composed of M by N (1 ≤ M ≤ 12; 1 ≤ N ...

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

    题目大意:给出一个M*N的矩阵,元素为0表示这个地方不能种玉米,为1表示这个地方能种玉米,现在规定所种的玉米不能相邻,即每行或者没列不能有相邻的玉米,问一共有多少种种植方法. 举个例子: 2 3 1 ...

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

    题目大意:一个矩形的草地,分为多个格子,有的格子可以有奶牛(标为1),有的格子不可以放置奶牛(标为0),计算摆放奶牛的方案数. 分析: f[i,j]表示第i行状态为j的方案总数. 状态转移方程f[i, ...

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

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

  6. poj - 3254 - Corn Fields (状态压缩)

    poj - 3254 - Corn Fields (状态压缩)超详细 参考了 @外出散步 的博客,在此基础上增加了说明 题意: 农夫有一块地,被划分为m行n列大小相等的格子,其中一些格子是可以放牧的( ...

  7. poj3254 状态压缩dp

    题意:给出一个n行m列的草地,1表示肥沃,0表示贫瘠,现在要把一些牛放在肥沃的草地上,但是要求所有牛不能相邻,问你有多少种放法.     分析:假如我们知道第 i-1 行的所有的可以放的情况,那么对于 ...

  8. POJ 3254 Corn Fields(状态压缩)

    一道状态压缩的题,错了好多次....应该先把满足的情况预处理出来 #include<iostream> #include<cstdio> #include<cstring ...

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

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

随机推荐

  1. css中很有用的属性

    有些css属性很实用,但不常用也就被忘记. 这里纪录了自己在项目中用过的一些. html,body{ -webkit-tap-highlight-color:transparent; } 这个的用途是 ...

  2. C#_基本类型

    1.C#中的值类型包括:简单类型.枚举类型和结构类型. 2.C#中的引用类型包括:类(class).接口(interface).数组.委托(delegate).object和string. 3.调试时 ...

  3. 尝试自己翻译了FreeCodeCamp的文章,技术方面多认识了几种技术,文章标题:Transparency in Action Free Code Camp is Now Open Source

    这是FreeCodeCamp其中一篇文章,趁着学习英文的时间,翻译这篇文章,其中讲到作者创建FCC过程,本文属于原创,第一次翻译,翻译还有诸多不足之处,请大家包含. 原文地址:https://medi ...

  4. Java this关键字

    this 关键字有三个应用: 1.this调用本类中的属性,也就是类中的成员变量 2.this调用本类中的其他方法 3.this调用本类中的其他构造方法,调用时要放在构造方法的首行 来看下面这段代码: ...

  5. Error:java:Compilation failed: internal java compiler error

    在IDEA中编译时出现这个错误:Error:java:Compilation failed: internal java compiler error! Information:Using javac ...

  6. 分享 rabbitMQ入门详解

    原文地址http://blog.csdn.net/cugb1004101218/article/details/21243927 目录(?)[-] rabbitMQ说明文档 rabbitMQ是什么 消 ...

  7. 阻止网页内部滚动条mousewheel事件冒泡

    function preventScroll(id){ var _this = document.getElementById(id); if(navigator.userAgent.indexOf( ...

  8. JavaNote01_变量 基本数据类型

    >主要内容: 变量的初始化.赋值.读写操作 8中基本数据类型(取值范围).整数的直接量(字面量)是哪种类型.浮点数的字面量是哪种类型 >变量 >>声明变量:开启一个存储单元,用 ...

  9. leetcode 397

    题目描述: Given a positive integer n and you can do operations as follow: If n is even, replace n with n ...

  10. noi 1.5 43:质因数分解

    描述 已知正整数 n 是两个不同的质数的乘积,试求出较大的那个质数. 输入 输入只有一行,包含一个正整数 n.对于60%的数据,6 ≤ n ≤ 1000.对于100%的数据,6 ≤ n ≤ 2*10^ ...