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. Phpexcel 多sheet ,设置

    多sheet 一定要县创建sheet foreach ($alias_name as $ca) { if ($sheet > 0) { $objExcel->createSheet(); ...

  2. 整理JRE瘦身或精简JRE

    http://blog.csdn.net/xiaoping8411/article/details/6973887 版权声明:本文为博主原创文章,未经博主允许不得转载. 前不久给朋友做了一个桌面程序, ...

  3. Windows Azure Storage Client Library 2.0 入门

    入门连接如下: http://gauravmantri.com/2012/11/17/storage-client-library-2-0-migrating-table-storage-code/

  4. 说说JSON和JSONP,浅析JSONP解决AJAX跨域问题

    说到AJAX就会不可避免的面临两个问题,第一个是AJAX以何种格式来交换数据?第二个是跨域的需求如何解决?这两个问题目前都有不同的解决方案,比如数据可以用自定义字符串或者用XML来描述,跨域可以通过服 ...

  5. jQuery源码学习(2):选择器初窥

    选择器初窥 代码架构: jQuery选择器可以依照传入数据的类型分为五大类: 传入字符串:$("div"), $("#id"), $(".div1&q ...

  6. Session保存到指定数据库中

    方法1:向数据库中添加session相关信息,可以使用官方工具 命令提示符cmd中执行: cd C:\Windows\Microsoft.NET\Framework\v4.0.30319       ...

  7. ul中li分列显示

    让ul中li分列显示,用li显示两列如下(要显视多列的自己想办法,哈哈): 2列 <ul> <li style="display:block;float:left;widt ...

  8. 把div 当文字来进行布局控制

    两边对齐 text-align: justify; text-justify: distribute-all-lines;/*ie6-8*/ text-align-last: justify;/* i ...

  9. handler的使用

    2014-04-15 10:45:06 简单学习了handler的使用. 昨天下载的问题,在手机上正常,在平板上不正常. 怀疑是网络的问题. 一直获得的流为空 2014-04-15 18:10:59 ...

  10. Mongodb 导出json 和csv 格式数据

    导出到json: $ mongoexport.exe  -d TestDB -c TestCollection -o  ./test.json 导出到csv: If you want to outpu ...