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. Mybatis操作数据时出现:java.sql.SQLSyntaxErrorException: Unknown column 'XXX' in 'field list'

    这个错误比较重要,而且很常见,故单独进行说明: Mybatis出现:Unknown column 'xxx' in 'field list' 先来看一下程序的内部: dao.addUser(" ...

  2. window启动目录

    启动文件目录 C:\Users\Administrator\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup

  3. kafka调优

    kafka调优:[root@bi-kafka-1 bin]# pwd/data/kafka-9092/bincat kafka-server-start.sh if [ "x$KAFKA_H ...

  4. [Python3] 039 语法调试

    目录 语法调试 1. 调试技术 2. pdb 调试 插一个 gdb 3.Pycharm 调试 4. 单元测试 语法调试 1. 调试技术 调试流程 单元测试 → 集成测试 → 交测试部 分类: 静态调试 ...

  5. [转帖](整理)GNU Hurd项目详解

    (整理)GNU Hurd项目详解 http://www.ha97.com/3188.html 发表于: 开源世界 | 作者: 博客教主 标签: GNU,Hurd,详解,项目 Hurd原本是要成为GNU ...

  6. Postgresql 监控sql之 pg_stat_statements模块

    postgresql.confpg_stat_statements.max = 1000000pg_stat_statements.track = allpg_stat_statements.trac ...

  7. 用Java实现对英文版《飘》的文件读取与写入操作

    从文件读入<飘>的英文版,并将结果输出到文件中 要求一: 实现对英文版<飘>的字母出现次数统计 package File; import java.io.FileInputSt ...

  8. mweb发布文章为什么默认TinyMCE编辑器?

    如果是通过 metaweblog api 发布的,需要在网站分类中添加 [Markdown] 标记

  9. python进程之间的通信——Queue

    我们知道进程之间的数据是互不影响的,但有时我们需要在进程之间通信,那怎么办呢? 认识Queue 可以使用multiprocessing模块的Queue实现多进程之间的数据传递,Queue本身是一个消息 ...

  10. 公司PL/SQL考核及小结

    一.数据库初始化脚本 -- Create table 学生信息 drop table HAND_STUDENT; create table HAND_STUDENT ( STUDENT_NO ) no ...