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. PAT 天梯赛 L2-005 集合相似度

    set的应用 题目链接 题解 有点像集合的交并操作,直接利用set进行处理,因为set有去重的功能,而且set是利用红黑树实现的,查找速度快O(logN). 代码如下: #include<cst ...

  2. JS操作DOM对象——JS基础知识(四)

    一.JavaScript的三个重要组成部分 (1)ECMAScript(欧洲计算机制造商协会) 制定JS的规范 (2)DOM(文档对象模型)重点学习对象 处理网页内容的方法和接口 (3)BOM(浏览器 ...

  3. Arduino线程库ProtoThreads

    参考: Arduino线程库ProtoThreads 一个“蝇量级” C 语言协程库

  4. ARM-LINUX学习笔记-1

    安装完linux之后记得系统更新,更新使用apt命令,如下(记得使用之前使用sudo -i 指令切换到root用户模式) apt-get update  更新系统软件源,相当于查找更新 apt-get ...

  5. 12、手把手教你Extjs5(十二)执行菜单命令在tabPanel中显示模块

    上面设计好了一个模块的主界面,下面通过菜单命令的执行来把这个模块加入到主界面当中.在MainModule.js中有一个函数,生成了当前的菜单数据: // 根据data.systemMenu生成菜单条和 ...

  6. MySQL 同步状态

    Exec_Master_Log_Pos: The position of the last event executed by the SQL thread from the master's bin ...

  7. 学习vi(1)

    原文地址:http://www.gentoo.org/doc/zh_cn/vi-guide.xml#doc_chap2 1.  新手上路 介绍 本教程将会向你展示如何使用vi──一个强大的可视化编辑器 ...

  8. Android源码编译jar包BUILD_JAVA_LIBRARY 与BUILD_STATIC_JAVA_LIBRARY的区别(一)

    一般情况下,在Android源码下编译一个jar包的典型makefile(Android.mk)如下: 在文件中加入以下内容: LOCAL_PATH:= $(call my-dir)#make jar ...

  9. SD卡初始化以及命令详解

    SD卡是嵌入式设备中很常用的一种存储设备,体积小,容量大,通讯简单,电路简单所以受到很多设备厂商的欢迎,主要用来记录设备运行过程中的各种信息,以及程序的各种配置信息,很是方便,有这样几点是需要知道的 ...

  10. LearningDocker--Chapter3--Building images

    This chapter is quite different from the earlier ones, and it is in this chapter to clearly describe ...