Corn Fields

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 13203   Accepted: 6921

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

第一道状态压缩dp,水一水
题目大意: 农夫有一块地,被划分为m行n列大小相等的格子,其中一些格子是可以放牧的(用1标记),农夫可以在这些格子里放牛,其他格子则不能放牛(用0标记),并且要求不可以使相邻格子都有牛。
现在输入数据给出这块地的大小及可否放牧的情况,求该农夫有多少种放牧方案可以选择(注意:任何格子都不放也是一种选择,不要忘记考虑!
 //2016.8.8
#include<iostream>
#include<cstdio>
#include<cstring> using namespace std; const int mod = ;
int arr[][];
int dp[][<<];//dp[i][j]表示第i行第j种状态时的方法数。
int state[<<];//记录可行的状态,即两两不相邻,以减少枚举次数。
int cur[];//根据输入记录该行的状态。
int len;//可行状态总数 bool ok(int sta)//判断状态两两不相邻
{
return (sta&(sta<<))==?true:false;
} void init(int m)//初始化可行状态
{
len = ;
for(int i = ; i < (<<m); i++)
if(ok(i))state[len++] = i;
} bool fit(int sta, int k)//判断状态sta是否满足第k行的状态要求
{
return (sta&cur[k])==?true:false;
} int main()
{
int n, m;
while(cin>>n>>m)
{
init(m);
for(int i = ; i < n; i++)
{
int feifa = ;
for(int j = ; j < m; j++)
{
scanf("%d", &arr[i][j]);
if(arr[i][j]==)feifa += (<<(m-j-));//0为不可以放牛,反向存为1
}
cur[i] = feifa;
}
memset(dp, , sizeof(dp));
for(int i = ; i < len; i++)//初始化第一行dp值
if(fit(state[i], ))
dp[][i] = ;
for(int i = ; i < n; i++)
{
for(int j = ; j < len; j++)
{
if(fit(state[j], i))//如果状态state[j]满足该行i的要求,把上一行可行状态的方法数加起来
{
for(int k = ; k < len; k++)//枚举上一行(第i-1行)的状态
{
if(!fit(state[k], i-))continue;//排除不满足i-1行要求的状态
if(state[k]&state[j])continue;//排除不满足状态state[j]的状态
dp[i][j] = (dp[i][j]+dp[i-][k])%mod;
}
}
}
}
int ans = ;
for(int i = ; i < len; i++)//答案是最后一行填各个状态的方法数之和
ans = (ans+dp[n-][i])%mod;
cout<<ans<<endl;
} return ;
}

POJ3254(入门状态压缩dp)的更多相关文章

  1. poj3254(状态压缩DP)

    poj3254 题意 给出一个01矩阵,1表示当前这个位置可以放牛,要求放牛的方案保证牛不能左右或上下相邻,求方案数. 分析 dp[S][i]: 表示到 i 行时的状态S(用二进制数表示),那么状态转 ...

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

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

  3. DP大作战—状态压缩dp

    题目描述 阿姆斯特朗回旋加速式阿姆斯特朗炮是一种非常厉害的武器,这种武器可以毁灭自身同行同列两个单位范围内的所有其他单位(其实就是十字型),听起来比红警里面的法国巨炮可是厉害多了.现在,零崎要在地图上 ...

  4. POJ1185 - 炮兵阵地(状态压缩DP)

    题目大意 中文的..直接搬过来... 司令部的将军们打算在N*M的网格地图上部署他们的炮兵部队.一个N*M的地图由N行M列组成,地图的每一格可能是山地(用"H" 表示),也可能是平 ...

  5. 『最短Hamilton路径 状态压缩DP』

    状压DP入门 最短Hamilton路径 Description 给定一张 n(n≤20) 个点的带权无向图,点从 0~n-1 标号,求起点 0 到终点 n-1 的最短Hamilton路径. Hamil ...

  6. poj 3311 floyd+dfs或状态压缩dp 两种方法

    Hie with the Pie Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6436   Accepted: 3470 ...

  7. 状态压缩dp poj 3254 hdu5045

    近来感觉状态压缩dp的强大性(灵活利用了二进制运算非常关键). . . 于是做了俩提来看看..毕竟队友是专业的dp.我仅仅是管中窥豹下而已.. 日后有机会再与之玩耍玩耍...ps:假设上天再给我一次机 ...

  8. 状态压缩DP(大佬写的很好,转来看)

    奉上大佬博客 https://blog.csdn.net/accry/article/details/6607703 动态规划本来就很抽象,状态的设定和状态的转移都不好把握,而状态压缩的动态规划解决的 ...

  9. hoj2662 状态压缩dp

    Pieces Assignment My Tags   (Edit)   Source : zhouguyue   Time limit : 1 sec   Memory limit : 64 M S ...

随机推荐

  1. ural1521 War Games 2

    War Games 2 Time limit: 1.0 secondMemory limit: 64 MB Background During the latest war games (this s ...

  2. Selenuim+Python之元素定位总结及实例说明

    网页自动化最基本的要求就是要定位到各个元素,然后才能对该元素进行各种操作(输入,点击,清除,提交等),所以笔者今天来总结下Selenuim+Python最基本的几种定位方式及实例说明,希望能帮助到大家 ...

  3. Bootstrap-dialog的使用(续Bootstrap Table)

    Bootstrap-dialog实现表格内容的增,删,改. 插件引入:必须先引入jquery和bootstrap和artTemplate. <link rel="stylesheet& ...

  4. coding菜鸟养成记

    http://www.cnblogs.com/xdp-gacl/category/563690.html http://www.cnblogs.com/vincent-blog/p/4402327.h ...

  5. VB.Net隐式转换和显式转换的方法(转)

    VB.Net隐式转换和显式转换的方法(转) “隐式转换”不需要源代码中的任何特殊语法.在下面的示例中,在将 k 的值赋给 q 之前,Visual Basic 将该值隐式转换成单精度浮点值.   Dim ...

  6. uboot移植前奏

    Tiny4412开发板硬件版本为:       底板:  Tiny4412/Super4412SDK 1506        核心板:Tiny4412 - 1412 1.下载u-boot源代码,建立u ...

  7. python threading模块中对于信号的抓取

    最近的物联网智能网关(树莓派)项目中遇到这样一个问题:要从多个底层串口读取发来的数据,并且做出相应的处理,对于每个串口的数据的读取我能想到的可以采用两种方式: 一种是采用轮询串口的方式,例如每3s向每 ...

  8. td文字过长部分显示,鼠标移动显示全部内容

    只要在该td中加上title属性,鼠标移到这里就会看到全部内容, 在td中加上div,属性设置如下,就能显示宽度为200px的内容,大于则隐藏.代码如下: <td title="我是代 ...

  9. php 利用socket上传文件

    php 利用socket上传文件 张映 发表于 2010-06-02 分类目录: php 一,利用fsockopen来上传文件 以前我写过一篇关于socket通信原理的博文http://blog.51 ...

  10. 基于RBAC的权限设计模型

    个部件模型组成,这4个部件模型分别是基本模型RBAC0(Core RBAC).角色分级模型RBAC1(Hierarchal RBAC).角色限制模型RBAC2(Constraint RBAC)和统一模 ...