状态压缩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
描述 在河上有一座独木桥,一只青蛙想沿着独木桥从河的一侧跳到另一侧.在桥上有一些石子,青蛙很讨厌踩在这些石子上.由于桥的长度和青蛙一次跳过的距离都是正整数,我们可以把独木桥上青蛙可能到达的点看成数轴上 ...
随机推荐
- win10 uwp 活动磁贴
本文翻译:https://mobileprogrammerblog.wordpress.com/2015/12/23/live-tiles-and-notifications-in-universal ...
- ROS命令
rospack find [package_name]功能:获取软件包的路径 例:运行 rospack find roscpp ,会返回 /opt/ros/indigo/share/roscppros ...
- Java IO(IO流)-1
IO流 第一部分 (outputStream/InputStream Writer/Redaer) IO流对象中输入和输出是相辅相成的,输出什么,就可以输入什么. IO的命名方式为前半部分能干什么,后 ...
- LeetCode 405. Convert a Number to Hexadecimal (把一个数转化为16进制)
Given an integer, write an algorithm to convert it to hexadecimal. For negative integer, two’s compl ...
- python函数前篇
函数:函数是指将一组语句的集合通过一个函数名封装起来,要想执行这个函数,只需调用其函数名即可 函数特性: 减少重复代码 使程序变得可扩展 使程序变得易维护 什么是函数? 函数就是具备某一特定功能的工具 ...
- gulp-prompt入个了门
gulp-prompt版本:0.4.1 源码:gulp-prompt 一.gulp-prompt的简介 gulp-prompt 是一个基于gulp的命令行提示. 我们可以用它来完成命令行中互动功能. ...
- 【前端GUI】——网站设计的重要知识点总结&思维导图(一)
前言:网页美术设计具有四大特点,分别为交互性.整合性.多维性以及动态性.完整的网页设计既需要试听元素,也需要版式设计,以求有效的传达信息.在设计的时候,设计者要学会利用框架,也要学会打破框架. 一.优 ...
- 通过命令行使用cl.exe编译器
转载http://www.cnblogs.com/mizhongqin/archive/2013/03/11/cmd_cl-exe_vs2010.html 与在IDE中编译相比,命令行模式编译速度更快 ...
- A - 棋盘问题 POJ - 1321
在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子的所有可行的摆放方案C. ...
- jumpserver安装详解
环境说明 主机为最小 安装的centos6.9 x86_64. [root@m01 ~]# cat /etc/redhat-release CentOS release 6.9 (Final) [ro ...