B - Corn Fields

Crawling in process... Crawling failed Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu

Description

 

Input

 

Output

 

Sample Input

 

Sample Output

 

Hint

 

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.

题意:

有一片地,有的地方可以种草,有的地方不能,并且相邻的两块不能同时种草,问总共有多少种种草的方法。

思路:

由于一行的状态可由前一行的状态推出,dp[i][j]+=dp[i-1][k],k,j表示此行的状态。第一道状压DP,虽然是照着别人的博客写出来的但确实学到了很多。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int MOD = 1e8;
int n, m, f[][ << ], mp[], g[][ << ];
int main()
{
while (~scanf("%d %d", &n, &m)) {
for (int i = ; i < n; ++i) {
for (int j = m - , x; j >= ; --j) {
scanf("%d", &x);
if (x) mp[i] |= ( << j);
}
}
for (int i = ; i < n; ++i) {
for (int j = ; j < ( << m); ++j) {
g[i][j] = ;
if (j & (j <<)) continue;
if (~mp[i] & j) continue;
//原来第i行的状态取反与j状态相与为1说明j状态在没草的地方放牛,所以不合法即~mat[i]&j
//判断j状态是否有相邻的两个1,若有即不合法,用j&(j<<1)判断,j<<1即j右移1位
//eg:01101--->11010,相与为1,即可判断出有相邻的1,不合法。
g[i][j] = ;
}
}
for (int j = ; j < ( << m); ++j) f[][j] = g[][j];
for (int i = ; i < n; ++i) {
for (int j = ; j < ( << m); ++j) {
f[i][j] = ;
if (!g[i][j]) continue;
for (int k = ; k < ( << m); ++k) {
if (!g[i - ][k]) continue;
if (j & k) continue; //i+1行的状态与i行的状态不冲突
f[i][j] = (f[i][j] + f[i - ][k]) % MOD;
}
}
}
int ans = ;
for (int j = ; j < ( << m); ++j) {
ans = (ans + f[n - ][j]) % MOD;
}
printf("%d\n", ans);
}
return ;
}

POJ 3254 状态压缩 DP的更多相关文章

  1. poj 3254 状态压缩DP

    思路:把每行的数当做是一个二进制串,0不变,1变或不变,找出所有的合法二进制形式表示的整数,即相邻不同为1,那么第i-1行与第i行的状态转移方程为dp[i][j]+=dp[i-1][k]: 这个方程得 ...

  2. poj 3254(状态压缩+动态规划)

    http://poj.org/problem?id=3254 题意:有一个n*m的农场(01矩阵),其中1表示种了草可以放牛,0表示没种草不能放牛,并且如果某个地方放了牛,它的上下左右四个方向都不能放 ...

  3. POJ 1185 状态压缩DP(转)

    1. 为何状态压缩: 棋盘规模为n*m,且m≤10,如果用一个int表示一行上棋子的状态,足以表示m≤10所要求的范围.故想到用int s[num].至于开多大的数组,可以自己用DFS搜索试试看:也可 ...

  4. POJ 1185 状态压缩DP 炮兵阵地

    题目直达车:   POJ 1185 炮兵阵地 分析: 列( <=10 )的数据比较小, 一般会想到状压DP. Ⅰ.如果一行10全个‘P’,满足题意的状态不超过60种(可手动枚举). Ⅱ.用DFS ...

  5. poj 2923(状态压缩dp)

    题意:就是给了你一些货物的重量,然后给了两辆车一次的载重,让你求出最少的运输次数. 分析:首先要从一辆车入手,搜出所有的一次能够运的所有状态,然后把两辆车的状态进行合并,最后就是解决了,有两种方法: ...

  6. poj 2688 状态压缩dp解tsp

    题意: 裸的tsp. 分析: 用bfs求出随意两点之间的距离后能够暴搜也能够用next_permutation水,但效率肯定不如状压dp.dp[s][u]表示从0出发訪问过s集合中的点.眼下在点u走过 ...

  7. Mondriaan's Dream(POJ 2411状态压缩dp)

    题意:用1*2的方格填充m*n的方格不能重叠,问有多少种填充方法 分析:dp[i][j]表示i行状态为j时的方案数,对于j,0表示该列竖放(影响下一行的该列),1表示横放成功(影响下一列)或上一列竖放 ...

  8. poj 2411 状态压缩dp

    思路:将每一行看做一个二进制位,那么所有的合法状态为相邻为1的个数一定要为偶数个.这样就可以先把所有的合法状态找到.由于没一层的合法状态都是一样的,那么可以用一个数组保存.由第i-1行到第i行的状态转 ...

  9. poj 3254 状态压缩

    Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15285   Accepted: 8033 Desc ...

随机推荐

  1. Java版office文档在线预览

    java将office文档pdf文档转换成swf文件在线预览 第一步,安装openoffice.org openoffice.org是一套sun的开源office办公套件,能在widows,linux ...

  2. python 项目配置虚拟环境

    # Windows 环境1, 安装 Visual C++ 2015 Build Tools, 依赖.Net Framework 4.6, 安装包位置 ./tools/windows/visualcpp ...

  3. lintcode-187-加油站

    187-加油站 在一条环路上有 N 个加油站,其中第 i 个加油站有汽油gas[i],并且从第_i_个加油站前往第_i_+1个加油站需要消耗汽油cost[i]. 你有一辆油箱容量无限大的汽车,现在要从 ...

  4. iOS开发UIColor,CGColor,CIColor三者的区别和联系

    最近看了看CoreGraphics的东西,看到关于CGColor的东西,于是就想着顺便看看UIColor,CIColor,弄清楚它们之间的区别和联系.下面我们分别看看它们三个的概念: 一.UIColo ...

  5. ASP.NET 最全的POST提交数据和接收数据 —— (1) 用url传参方式

    //1.对象提交,字典方式 //接口方:public ActionResult GetArry(Car model) public void PostResponse() { HttpWebReque ...

  6. alpha阶段个人总结(201521123031林庭亦)

    一.个人总结 第一部分:硬的问题 第二部分:软的问题,在成长路上学到了什么? 1 当你看到不靠谱的设计.糟糕的代码.过时的文档和测试用例的时候,不要想 "既然别人的代码已经这样了,我的代码也 ...

  7. centos7 安装 httpd并打开测试页

    systemctl start firewalld.service#启动firewallsystemctl stop firewalld.service#停止firewallsystemctl dis ...

  8. 如何彻底解决adb 5037端口被占用

    在进行安卓开发的时候是不是经常碰到adb端口被占用的情况? 解决这个问题的方法很简单,只需要设置一个系统环境变量就可以搞定. 设置方法: 增加系统环境变量变量名称:ADNROID_ADB_SERVER ...

  9. 【Python】Python 过滤列表

    如你所知,Python 具有通过列表解析将列表映射到其它列表的强大能力.这种能力同过滤机制结合使用,使列表中的有些元素被映射的同时跳过另外一些元素.过滤列表语法: [mapping-expressio ...

  10. BZOJ3139/BZOJ1306 HNOI2013比赛/CQOI2009循环赛(搜索)

    搜索好难啊. 1.对于每个分数集合记忆化. 2.某人得分超过总分,剪枝. 3.某人之后全赢也无法达到总分,剪枝. 4.每有一场比赛分出胜负总分会多三分,而平局则会多两分.某人的分出胜负场次或平局场次超 ...