Bubble Shooter

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1057    Accepted Submission(s): 454

Problem Description
Bubble shooter is a popular game. You can find a lot of versions from the Internet.

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.

 
Input
There
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).
 
Output
For each test case, output an integer indicating how many bubbles will explode.
 
Sample Input
2 2 2 1
aa
a
3 3 3 3
aaa
ba
bba
3 3 3 1
aaa
ba
bba
3 3 3 3
aaa
Ea
aab
 
Sample Output
3
8
3
0
 
Author
JIN, Tianpeng
 
Source
 
题意:玩泡泡龙,开始是所有的球都是通过六个方向连在一起的,规则是如果现在指定的位置有至少三个球在六个方向组成一个连通分量,这些球就是可以消掉的,消掉这些球之后,如果有些球没有和最上面的那一层相连了,那么就会掉下去,现在给出矩阵的大小和指定的位置,问最多可以消掉多少球?
题解:这题主要是要弄清泡泡龙六个方向是哪里,要分奇数行和偶数行进行讨论,如果是奇数行,那么他可以和(x,y-1),(x,y+1),(x-1,y-1),(x-1,y),(x+1,y-1),(x+1,y)相连,如果是偶数行,它可以和(x,y-1),(x,y+1),(x+1,y+1),(x+1,y),(x-1,y+1),(x-1,y)相连,先从初始位置做一次bfs,将所有相同颜色并且相连的球标记计数,然后从第一行开始做第二次bfs,将所有能够连通的块都连通,最后统计哪些没有被标记,那么这些球就会掉下去,计数即可.
#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)的更多相关文章

  1. hdu 4531 bfs(略难)

    题目链接:点我 第一次不太清楚怎么判重,现在懂了,等下次再做 /* *HDU 4531 *BFS *注意判重 */ #include <stdio.h> #include <stri ...

  2. HDU 2822 (BFS+优先队列)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2822 题目大意:X消耗0,.消耗1, 求起点到终点最短消耗 解题思路: 每层BFS的结点,优先级不同 ...

  3. HDU 1180 (BFS搜索)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1180 题目大意:迷宫中有一堆楼梯,楼梯横竖变化.这些楼梯在奇数时间会变成相反状态,通过楼梯会顺便到达 ...

  4. HDU 2531 (BFS搜索)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2531 题目大意: 你的身体占据多个点.每次移动全部的点,不能撞到障碍点,问撞到目标点块(多个点)的最 ...

  5. HDU 5025 (BFS+记忆化状压搜索)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5025 题目大意: 迷宫中孙悟空救唐僧,可以走回头路.必须收集完钥匙,且必须按顺序收集.迷宫中还有蛇, ...

  6. HDU 1429 (BFS+记忆化状压搜索)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1429 题目大意:最短时间内出迷宫,可以走回头路,迷宫内有不同的门,对应不同的钥匙. 解题思路: 要是 ...

  7. HDU 1026 (BFS搜索+优先队列+记录方案)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1026 题目大意:最短时间内出迷宫.迷宫里要杀怪,每个怪有一定HP,也就是说要耗一定时.输出方案. 解 ...

  8. HDU 1312 (BFS搜索模板题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1312 题目大意:问迷宫中有多少个点被访问. 解题思路: DFS肯定能水过去的.这里就拍了一下BFS. ...

  9. HDU 1242 (BFS搜索+优先队列)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1242 题目大意:多个起点到一个终点,普通点耗时1,特殊点耗时2,求到达终点的最少耗时. 解题思路: ...

随机推荐

  1. mybatis的Mapper代理原理

    前言:在mybatis的使用中,我们会习惯采用XXMapper.java+XXMapper.xml(两个文件的名字必须保持一致)的模式来开发dao层,那么问题来了,在XXMapper的文件里只有接口, ...

  2. 【题解】Crash的数字表格 BZOJ 2154 莫比乌斯反演

    题目传送门 http://www.lydsy.com/JudgeOnline/problem.php?id=2154 人生中第一道自己做出来的莫比乌斯反演 人生中第一篇用LaTeX写数学公式的博客 大 ...

  3. 在IAR使用FreeRTOS出现Error[Pa045]: function "XXX" has no prototype

    FreeRTOS官方例程中设置了需要“Require prototype”,所以每个函数(除了main函数)都需要函数声明,其中对于无形参的函数声明要加void,比如void led_init(voi ...

  4. <audio>标签HTML5音乐播放器

    <audio>标签:用于在文档中表示音频内容.利用它,你可以在你的个人网站上放一首你喜欢的歌.    <audio src="music.mp3">< ...

  5. 关于mysql 删除数据后物理空间未释放

    转载自:http://www.cnblogs.com/shawnloong/archive/2013/02/07/2908911.html OPTIMIZE TABLE 当您的库中删除了大量的数据后, ...

  6. Eclipse中安装Tomcat

    1. 下载Tomcat并安装: http://tomcat.apache.org/download-60.cgi 2. 下载最新Eclipse的Tomacat插件: http://www.eclips ...

  7. [洛谷P4774] [NOI2018]屠龙勇士

    洛谷题目链接:[NOI2018]屠龙勇士 因为markdown复制过来有点炸格式,所以看题目请戳上面. 题解: 因为杀死一条龙的条件是在攻击\(x\)次,龙恢复\(y\)次血量\((y\in N^{* ...

  8. elasticsearch中Node的创建

    要连接到集群,首先要告诉集群:你是谁,你有什么特征.在es中体现为实例化节点,elasticsearch通过org.elasticsearch.node.NodeBuilder的build()或者no ...

  9. java分页通用篇

    一.创建分页通用类 package com.dkyw.util; import java.util.List; public class Page<T> { private int tot ...

  10. python碎片记录(二)

    1.字典中嵌套字典使用 dict={'a':{1:2,2:3}} print(dict) print(dict['a'][2]) 输出如下: {'a': {1: 2, 2: 3}} 3  2.元组与l ...