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

之前做了一下“铺地砖”的题,然后看别人用插头DP做,代码很简洁,于是也打算学习一下插头DP。

这个题应该是比较基础,反正1A了,爽歪歪。

不过第一次写的可能有些丑。

#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<cmath>
using namespace std;
const int Mod=;
int dp[][<<],ans,a[][],m,n;
bool check(int u,int x,int y)
{
for(int i=;i<=m;i++) if((x&(<<(i-)))&&(x&(<<(i-)))) return false;
for(int i=;i<=m;i++) if((y&(<<(i-)))&&(y&(<<(i-)))) return false;
for(int i=;i<=m;i++) if(!a[u-][i]&&(x&(<<(i-)))) return false;
for(int i=;i<=m;i++) if(!a[u][i]&&(y&(<<(i-)))) return false;
for(int i=;i<=m;i++) if((x&(<<(i-)))&&(y&(<<(i-)))) return false;
return true;
}
int main()
{
int i,j,k;
while(~scanf("%d%d",&n,&m)){
for(i=;i<=n;i++)
for(j=m;j>=;j--)
scanf("%d",&a[i][j]);
dp[][]=;
for(i=;i<=n;i++){
for(j=;j<<<m;j++)
for(k=;k<<<m;k++)
if(check(i,j,k))
dp[i][k]=(dp[i][k]+dp[i-][j])%Mod;
}
for(i=;i<<<m;i++) ans=(ans+dp[n][i])%Mod;
printf("%d\n",ans);
}
return ;
}

POJ3254Corn Fields (状态压缩or插头DP)的更多相关文章

  1. POJ3254Corn Fields(状态压缩DP入门)

    题目链接 题意:一个矩阵里有很多格子,每个格子有两种状态,可以放牧和不可以放牧,可以放牧用1表示,否则用0表示,在这块牧场放牛,要求两个相邻的方格不能同时放牛,即牛与牛不能相邻.问有多少种放牛方案(一 ...

  2. POJ2411Mondriaan's Dream(DP+状态压缩 or 插头DP)

    问题: Squares and rectangles fascinated the famous Dutch painter Piet Mondriaan. One night, after prod ...

  3. POJ3254Corn Fields——状态压缩dp

    题目:http://poj.org/problem?id=3254 1.枚举行: 2.把有影响的“放不放牛”加入参数中,用二进制数表示该位置放不放牛,再用十进制数表示二进制数: 3.优美的预处理lis ...

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

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

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

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

  6. poj Corn Fields 状态压缩dp。

    Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 5763   Accepted: 3052 Descr ...

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

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

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

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

  9. POJ 3254 Corn Fields状态压缩DP

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

随机推荐

  1. 对js原型对象、实例化对象及prototype属性的一些见解

    什么是原型对象? 请看下面的代码,我们以各种姿势,创建了几个方法! function fn1() { } var fn2 = function () { } var fn3 = new Functio ...

  2. 461. 汉明距离(Hamming Distance)leetcode

    首先附上题目链接: https://leetcode-cn.com/problems/hamming-distance/ 一:题目 两个整数之间的汉明距离指的是这两个数字对应二进制位不同的位置的数目. ...

  3. typeScript入门配置

    typeScript是有微软开发的一款开源的编程语言. TypeScript是JavaScript的一个超集,从今天数以百万计的JavaScript开发者所熟悉的语法和语义开始.可以使用现有的Java ...

  4. python高级 之(二) --- 类装饰器

    装饰器-初级 在不改变原有函数逻辑功能的基础上,为函数添加新的逻辑功能.使代码可读性更高.结构更加清晰.冗余度更低 简介 """ 闭包: 函数嵌套的格式就是闭包.写装饰器 ...

  5. 【Linux开发】linux设备驱动归纳总结(八):4.总线热插拔

    linux设备驱动归纳总结(八):4.总线热插拔 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ...

  6. plpython 中文分词Windows 版

    windows 下安装版本匹配python-3.4.3.amd64.msipostgresql-10.1-2-windows-x64.exe create language plpython3u;se ...

  7. Centos 安装Pycharm 并移动到桌面。

    版权声明:版权所有.未经同意不得转发,装载 https://blog.csdn.net/limingyue0312/article/details/81805826 1.下载pycharm软件包 网页 ...

  8. MySQL日期时间函数大全

    1.获取当前时间+日期 函数now() mysql> select now(); +---------------------+ | now() | +--------------------- ...

  9. c++学习笔记之类和对象(三、static静态成员变量和静态成员函数)

    一.static静态成员变量 对象的内存中包含了成员变量,不同的对象占用不同的内存,这使得不同对象的成员变量相互独立,它们的值不受其他对象的影响.是有时候我们希望在多个对象之间共享数据,对象 a 改变 ...

  10. 牛客 2C 圈圈

    题面: shy有一个队列a[1], a[2],…,a[n].现在我们不停地把头上的元素放到尾巴上.在这过程中我们会得到n个不同的队列,每个队列都是a[k],a[k+1],…,a[n],a[1],…,a ...