状态压缩dp第一题
标签: ACM
题目:
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.
题意:
第一行输入草地长宽,后面输入该地方能不能使用,输出可以使用的所有方案
解题思路
从例题来看第一层有五种可能分别为000,001,010,100,101
,都标记为1种可能
第二层可以有000
与010
两种状态,但是与上一层比较000
与上一层五种状态都不冲突标记为5种可能,而010
与上一层010
状态冲突,所以标记为4种可能
第二层为最底层,将最后一层的可能性全部相加得到9
使用状态压缩,将所有可能存在状态储存到数组里面
然后从第一层存在的状态标记为1
从第二层开始遍历到最后一层,第二层存在的状态且不和上一层冲突将上一层的状态标记加到该层的标记上
遍历到最后一层时将最后一层的状态总和加起来就是所有的可能性
注:根据题意答案要对100000000取余
AC代码
#include <iostream>
#include <string.h>
#define M 4100
#define N 15
using namespace std;
int map[N]; //该行的输入状态
int m,n;
int dp[N][M];
int p;//该列最大状态
int s[M]; //储存每一行拥有的状态最大4096种状态
int mod=100000000;
bool checkLine(int i) //该行是否满足条件
{
return !(i&(i>>1));
}
bool checkTwoLine(int i,int j) //与上一行是否冲突
{
return !(i&j);
}
bool include(int i,int j) //是否是包含关系
{
return ((i|j)==i);
}
void init()
{
p=0;
int i,j;
for(i=0;i<(1<<m);i++)
if(checkLine(i))
s[p++]=i;
}
void solve()
{
int i,j,k;
int ans=0;
for(i=0;i<p;i++)
if(include(map[0],s[i]))
dp[0][i]=1;
for(i=1;i<n;i++)
for(j=0;j<p;j++) //该行的状态
{
if(!include(map[i],s[j]))
continue;
else
for(k=0;k<p;k++) //上一行的状态
{
if(include(map[i-1],s[k])&&checkTwoLine(s[j],s[k]))
dp[i][j]=(dp[i][j]+dp[i-1][k])%mod;
}
}
for(i=0;i<p;i++)
ans=(ans+dp[n-1][i])%mod;
cout<<ans<<endl;
}
int main()
{
while(cin>>n>>m)
{
memset(map,0,sizeof(map));
int i,j;
for(i=0;i<n;i++)
for(j=0;j<m;j++)
{
int plant;cin>>plant;
if(plant){
map[i]+=(1<<j); //将输入转换成二进制储存
}
}
init();
solve();
}
return 0;
}
状态压缩dp第一题的更多相关文章
- 状态压缩---状态压缩dp第一题
标签: ACM 题目: Farmer John has purchased a lush new rectangular pasture composed of M by N (1 ≤ M ≤ 12; ...
- POJ Corn Fields 状态压缩DP基础题
题目链接:http://poj.org/problem?id=3254 题目大意(名称什么的可能不一样,不过表达的意思还是一样的): 种玉米 王小二从小学一年级到现在每次考试都是班级倒数第一名,他的爸 ...
- 状态压缩DP入门题
. /*本题为状态压缩题 题目大意 : 一个矩阵里有很多格子,每个格子有两种状态,可以放牧和不可以放牧, 可以放牧用1表示,否则用0表示,在这块牧场放牛,要求两个相邻的方 格不能同时放牛(不包括斜着的 ...
- Hdu-1565 方格取数(1) (状态压缩dp入门题
方格取数(1) Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...
- [NOIP2016]愤怒的小鸟 状态压缩dp
题目描述 Kiana最近沉迷于一款神奇的游戏无法自拔. 简单来说,这款游戏是在一个平面上进行的. 有一架弹弓位于(0,0)处,每次Kiana可以用它向第一象限发射一只红色的小鸟,小鸟们的飞行轨迹均为形 ...
- Kefa and Dishes(CodeForces580D)【状态压缩DP】
状态压缩DP裸题,比赛的时候没反应过来,进行了n次枚举起点的solve,导致超时. #include<cstdio> #include<iostream> #include&l ...
- 状态压缩dp增量统计贡献——cf1238E(好题)
这题的状态设计非常巧妙,因为dp[S]表示的并非当前正确的值,而是维护一个中间量,这个中间量在到达末状态时才正确 当然官方的题解其实更加直观,只不过理解起来其实有点困难 /* 给定一个串s,字符集为2 ...
- BZOJ-1226 学校食堂Dining 状态压缩DP
1226: [SDOI2009]学校食堂Dining Time Limit: 10 Sec Memory Limit: 259 MB Submit: 588 Solved: 360 [Submit][ ...
- Vijos 1002 过河 状态压缩DP
描述 在河上有一座独木桥,一只青蛙想沿着独木桥从河的一侧跳到另一侧.在桥上有一些石子,青蛙很讨厌踩在这些石子上.由于桥的长度和青蛙一次跳过的距离都是正整数,我们可以把独木桥上青蛙可能到达的点看成数轴上 ...
随机推荐
- vim下单行长文本的时候卡顿解决办法
在vim编辑文件时,若单行过长,可能会导致vim卡顿,严重影响使用体验 估计是syntax匹配效率过滥导致.. 偶尔发现了一个临时的解决办法就是关掉syntax然后再打开,即在命令模式下 :synta ...
- WebService的简单运用添加删除
WebService是一种跨编程语言和跨操作系统平台的远程调用技术,简单来说就是将数据存储到项目的文件夹下 .NET中基于DOM核心类 XmlDocument 表示一个XML文档 XmlNode表示X ...
- 423. Reconstruct Original Digits from English (leetcode)
Given a non-empty string containing an out-of-order English representation of digits 0-9, output the ...
- 更新Android Studio 3.0碰到的问题
更新完后试下运行正在维护的旧项目,出现各种错误,因为后来发现问题不在这,所以没记完整,大概如下: A larger heap for the Gradle daemon is recommended ...
- [译]ASP.NET Core 2.0 网址重定向
问题 如何在ASP.NET Core 2.0中实现网址重定向? 答案 新建一个空项目,在Startup.cs文件中,配置RewriteOptions参数并添加网址重定向中间件(UseRewriter) ...
- rwx对于文件和目录的意义
1.对于文件 r:可读. w:可以编辑,可以修改. x:可以执行.在windows中,可执行指的是.exe,.bat等这些后缀结尾的文件,在linux没有这种限制. 2.对于目录 r:表示可以用ls命 ...
- Mr. Frog’s Game
Mr. Frog’s Game Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)T ...
- js验证是否为数字的总结(转)
作者: 字体:[增加 减小] 类型:转载 时间:2013-04-14我要评论 js验证是否为数字的总结,需要的朋友可以参考一下 js验证是否为数字,最简单的方法: isNaN函数的使用: functi ...
- java注解(Annotation)
本文转载自http://www.cnblogs.com/xdp-gacl/p/3622275.html 一.认识注解 注解(Annotation)很重要,未来的开发模式都是基于注解的,JPA是基于注解 ...
- ACID 数据库正确执行四要素
ACID:数据库事务正确执行所必须满足的四个基本要素的缩写: 原子性(atomicity,或叫不可分割性),一致性(consistency),隔离性(isolation,又称独立性),持久性(dura ...