hdu 1547(BFS)
Bubble Shooter
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1057 Accepted Submission(s): 454
The
goal of this game is to clean the bubbles off the field. Every time you
just point the cannon to where you want the next bubble to go, and if
three or more of bubbles with the same color came together (including
the newly shot bubble), they will detonate. After the first explode, if
some bubbles are disconnected from the bubble(s) in the topmost row,
they will explode too.
In this problem, you will be given an
arranged situation of bubbles in the field and the newly shot bubble.
Your program should output the total number of bubbles that will
explode.
are multiple test cases. Each test case begins with four integers H
(the height of the field, 2 <= H <= 100), W (the width of the
field, 2 <= W <= 100, in the picture above, W is 10), h (the
vertical position of the newly shot bubble, count from top to bottom,
and the topmost is counted as 1) and w (the horizontal position of the
newly shot bubble, count from left to right, and the leftmost is counted
as 1).
Then H lines follow, the odd lines will contain W characters
while the even lines will contain W-1 characters (refer to the picture
above). Each character will be either a lowercase from 'a' to 'z'
indicating the color of the bubble in that position, or a capital letter
'E' indicating an empty position. You may assure the arranged situation
is always valid (all the bubbles are directly or indirectly connected
with at least one bubble in the topmost row, and the position of newly
shot bubble is never empty).
aa
a
3 3 3 3
aaa
ba
bba
3 3 3 1
aaa
ba
bba
3 3 3 3
aaa
Ea
aab
8
3
0
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#include <stdlib.h>
using namespace std;
const int N = ;
int n,m,a,b;
char graph[N][N];
bool vis[N][N];
struct Node{
int x,y;
};
bool check(int x,int y,char c){
if(x%){
if(x<||x>n||y<||y>m||vis[x][y]||graph[x][y]=='E'||graph[x][y]!=c) return false;
}else{
if(x<||x>n||y<||y>=m||vis[x][y]||graph[x][y]=='E'||graph[x][y]!=c) return false;
}
return true;
}
bool check2(int x,int y){
if(x%){
if(x<||x>n||y<||y>m||vis[x][y]||graph[x][y]=='E') return false;
}else{
if(x<||x>n||y<||y>=m||vis[x][y]||graph[x][y]=='E') return false;
}
return true;
}
int dir1[][]={{,-},{,},{,},{,},{-,},{-,}}; ///偶数行
int dir2[][]={{,-},{,},{-,-},{-,},{,-},{,}}; ///奇数行
void bfs(){
queue<Node> q;
Node s;
s.x = a,s.y = b;
vis[s.x][s.y] = true;
q.push(s);
while(!q.empty()){
Node now = q.front();
q.pop();
Node next;
if(now.x%==){
for(int i=;i<;i++){
next.x = now.x+dir1[i][];
next.y = now.y+dir1[i][];
if(!check(next.x,next.y,graph[now.x][now.y])) continue;
vis[next.x][next.y] = true;
q.push(next);
}
}else{
for(int i=;i<;i++){
next.x = now.x+dir2[i][];
next.y = now.y+dir2[i][];
if(!check(next.x,next.y,graph[now.x][now.y])) continue;
vis[next.x][next.y] = true;
q.push(next);
}
}
}
}
void bfs2(int k){
queue<Node> q;
Node s;
s.x = ,s.y = k;
vis[s.x][s.y] = true;
q.push(s);
while(!q.empty()){
Node now = q.front();
q.pop();
Node next;
if(now.x%==){
for(int i=;i<;i++){
next.x = now.x+dir1[i][];
next.y = now.y+dir1[i][];
if(!check2(next.x,next.y)) continue;
vis[next.x][next.y] = true;
q.push(next);
}
}else{
for(int i=;i<;i++){
next.x = now.x+dir2[i][];
next.y = now.y+dir2[i][];
if(!check2(next.x,next.y)) continue;
vis[next.x][next.y] = true;
q.push(next);
}
}
}
}
int main(){
while(scanf("%d%d%d%d",&n,&m,&a,&b)!=EOF){
memset(graph,,sizeof(graph));
for(int i=;i<=n;i++){
scanf("%s",graph[i]+);
}
memset(vis,false,sizeof(vis));
bfs();
int ans = ;
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
if(vis[i][j]) ans++;
}
}
if(ans<){
printf("%d\n",);
continue;
}
for(int i=;i<=m;i++){
if(graph[][i]=='E'||vis[][i]) continue;
bfs2(i);
}
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
if(i%==&&j==m) continue;
if(graph[i][j]=='E') continue;
if(!vis[i][j]) ans++;
}
}
printf("%d\n",ans);
}
}
hdu 1547(BFS)的更多相关文章
- hdu 4531 bfs(略难)
题目链接:点我 第一次不太清楚怎么判重,现在懂了,等下次再做 /* *HDU 4531 *BFS *注意判重 */ #include <stdio.h> #include <stri ...
- HDU 2822 (BFS+优先队列)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2822 题目大意:X消耗0,.消耗1, 求起点到终点最短消耗 解题思路: 每层BFS的结点,优先级不同 ...
- HDU 1180 (BFS搜索)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1180 题目大意:迷宫中有一堆楼梯,楼梯横竖变化.这些楼梯在奇数时间会变成相反状态,通过楼梯会顺便到达 ...
- HDU 2531 (BFS搜索)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2531 题目大意: 你的身体占据多个点.每次移动全部的点,不能撞到障碍点,问撞到目标点块(多个点)的最 ...
- HDU 5025 (BFS+记忆化状压搜索)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5025 题目大意: 迷宫中孙悟空救唐僧,可以走回头路.必须收集完钥匙,且必须按顺序收集.迷宫中还有蛇, ...
- HDU 1429 (BFS+记忆化状压搜索)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1429 题目大意:最短时间内出迷宫,可以走回头路,迷宫内有不同的门,对应不同的钥匙. 解题思路: 要是 ...
- HDU 1026 (BFS搜索+优先队列+记录方案)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1026 题目大意:最短时间内出迷宫.迷宫里要杀怪,每个怪有一定HP,也就是说要耗一定时.输出方案. 解 ...
- HDU 1312 (BFS搜索模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1312 题目大意:问迷宫中有多少个点被访问. 解题思路: DFS肯定能水过去的.这里就拍了一下BFS. ...
- HDU 1242 (BFS搜索+优先队列)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1242 题目大意:多个起点到一个终点,普通点耗时1,特殊点耗时2,求到达终点的最少耗时. 解题思路: ...
随机推荐
- redux connect的浅比较说明
redux的connect方法是一个高阶组件,对包装的组件会在ShouldComponentUpdate中实现一个默认的浅比较. connect形式如下: connect([mapStateToPro ...
- android getpost代码
GetPostUtil public class GetPostUtil { /** * 向指定URL发送GET方法的请求 * * @param url * 发送请求的URL * @param par ...
- HDU3524 数论
Perfect Squares Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- Hibernate入门(4)- Hibernate数据操作
Hibernate加载数据 Session.get(Class clazz, Serializable id) clazz:需要加载对象的类,例如:User.class id:查询条件(实现了序列化接 ...
- 可以随时拿取spring容器中Bean的工具类
前言 在Spring帮我们管理bean后,编写一些工具类的时候需要从容器中拿到一些对象来做一些操作,比如字典缓存工具类,在没有找到字典缓存时,需要dao对象从数据库load一次,再次存入缓存中.此时需 ...
- js的alert抛出之后怎么让页面停止执行?
方法: 1.如果是form的submit提交,如果要停止,则返回false:如果提交,则返回true就行了. 2.如果是手工跳转的方式,则如果要停止,则不执行跳转代码:如果要提交,则执行跳转代码 示例 ...
- 动态规划:区间DP与环形DP
区间型动态规划的典型例题是石子归并,同时使用记忆化搜索实现区间动归是一种比较容易实现的方式,避免了循环数组实现的时候一些边界的判断 n堆石子排列成一条线,我们可以将相邻的两堆石子进行合并,合并之后需要 ...
- centos6.8配置FTP普通用户除了家目录外还能访问其他目录
今天有个需求,使用ftp服务搭建一个文件共享服务器,每个普通用户除了能访问自己家目录的东西,还能访问一个公共的目录.配置步骤如下: 环境如下: 创建用户并配置密码(使用默认家目录/home) user ...
- 【BZOJ4236】JOIOJI [DP]
JOIOJI Time Limit: 10 Sec Memory Limit: 256 MB[Submit][Status][Discuss] Description JOIOJI桑是JOI君的叔叔 ...
- UIImageView属性---iOS-Apple苹果官方文档翻译
本系列所有开发文档翻译链接地址:iOS7开发-Apple苹果iPhone开发Xcode官方文档翻译PDF下载地址 //转载请注明出处--本文永久链接:http://www.cnblogs.com/C ...