Bubble Shooter

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

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
 
题意:输入n行m列,和发出小球的到达的位置h,w,用a~z表示不同颜色的小球,E表示该位置为空,另外奇数行有m个小球位置,偶数行有m-1个小球位置

求发射出该小球后掉落的小球数量有多少,已知相同颜色的小球连通数量>=3就会掉落和悬空的小球也会掉落(即周围没有小球与它相连)

分析:先从发射出的小球那开始广搜,判断相同颜色连通的数量是否>=3,是则将这些位置标记为空

然后对第一行小球位置开始搜索,所有搜索到的小球都不是悬空的,即不会掉落,没搜索到的会掉落

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
#include<map>
#include<iomanip>
#define INF 99999999
using namespace std; const int MAX=100+10;
char Map[MAX][MAX];
int n,m,sx,sy,s[MAX*MAX];
int dir0[6][2]={0,1,0,-1,1,0,-1,0,-1,-1,1,-1};//偶数行能走得方向(行下标从0开始)
int dir1[6][2]={0,1,0,-1,1,0,-1,0,-1,1,1,1};//奇数行能走得方向 int BFS(bool flag,int sx,int sy){
char ch=Map[sx][sy];
Map[sx][sy]='E';
int sum=0;
queue<int>q;
int oq,a,b;
q.push(sx*m+sy);//将第x*n+y个点入队
while(!q.empty()){
oq=q.front();
q.pop();
if(flag)s[sum]=oq;//先记录点,如果sum<3则需要还原点
++sum;
int x=oq/m,y=oq%m;
for(int i=0;i<6;++i){
if(x%2)a=x+dir1[i][0],b=y+dir1[i][1];
else a=x+dir0[i][0],b=y+dir0[i][1];
if(a<0 || b<0 || a>=n || (a%2 && b>=m-1) || b>=m)continue;
if(Map[a][b] == 'E')continue;//表示该点为空
if(flag && Map[a][b] != ch)continue;//查找周围连通且相同颜色的球
Map[a][b]='E';
q.push(a*m+b);
}
}
if(flag && sum<3){
for(int i=0;i<sum;++i)Map[s[i]/m][s[i]%m]=ch;
}
return sum;
} int main(){
while(cin>>n>>m>>sx>>sy){
--sx,--sy;
for(int i=0;i<n;++i)cin>>Map[i];
int ans=0,sum=0;//sum表示总的球数
for(int i=0;i<n;++i){
for(int j=0;j<m;++j){
if(Map[i][j]>='a' && Map[i][j]<='z')++sum;
}
}
BFS(true,sx,sy);
for(int i=0;i<m;++i){
if(Map[0][i] != 'E')ans+=BFS(false,0,i);//查询与顶上连通的球
}
cout<<sum-ans<<endl;
}
return 0;
}

hdu1547之BFS的更多相关文章

  1. 图的遍历(搜索)算法(深度优先算法DFS和广度优先算法BFS)

    图的遍历的定义: 从图的某个顶点出发访问遍图中所有顶点,且每个顶点仅被访问一次.(连通图与非连通图) 深度优先遍历(DFS): 1.访问指定的起始顶点: 2.若当前访问的顶点的邻接顶点有未被访问的,则 ...

  2. 【BZOJ-1656】The Grove 树木 BFS + 射线法

    1656: [Usaco2006 Jan] The Grove 树木 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 186  Solved: 118[Su ...

  3. POJ 3278 Catch That Cow(bfs)

    传送门 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 80273   Accepted: 25 ...

  4. POJ 2251 Dungeon Master(3D迷宫 bfs)

    传送门 Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 28416   Accepted: 11 ...

  5. Sicily 1215: 脱离地牢(BFS)

    这道题按照题意直接BFS即可,主要要注意题意中的相遇是指两种情况:一种是同时到达同一格子,另一种是在移动时相遇,如Paris在(1,2),而Helen在(1,2),若下一步Paris到达(1,1),而 ...

  6. Sicily 1048: Inverso(BFS)

    题意是给出一个3*3的黑白网格,每点击其中一格就会使某些格子的颜色发生转变,求达到目标状态网格的操作.可用BFS搜索解答,用vector储存每次的操作 #include<bits/stdc++. ...

  7. Sicily 1444: Prime Path(BFS)

    题意为给出两个四位素数A.B,每次只能对A的某一位数字进行修改,使它成为另一个四位的素数,问最少经过多少操作,能使A变到B.可以直接进行BFS搜索 #include<bits/stdc++.h& ...

  8. Sicily 1051: 魔板(BFS+排重)

    相对1150题来说,这道题的N可能超过10,所以需要进行排重,即相同状态的魔板不要重复压倒队列里,这里我用map储存操作过的状态,也可以用康托编码来储存状态,这样时间缩短为0.03秒.关于康托展开可以 ...

  9. Sicily 1150: 简单魔板(BFS)

    此题可以使用BFS进行解答,使用8位的十进制数来储存魔板的状态,用BFS进行搜索即可 #include <bits/stdc++.h> using namespace std; int o ...

随机推荐

  1. php单例模式深入讲解

    避免多次初始化数据库连接DAO 需要多次初始化数据库连接的场景 场景1: 首先PHP单例模式我觉得只是针对单次页面级请求时出现多个应用场景并需要共享同一对象资源时是非常有意义的 一个类A需要调用多个类 ...

  2. 关于android:screenOrientation="portrait"等

    1.android:screenOrientation="portrait",表示当切换横竖屏的时候,屏幕的内容始终以竖屏显示,而不会根据屏幕的方向来显示内容. 2.插入个Andr ...

  3. java JNI 的实现(1)-又进一步加深对JVM实现的理解

    目录 概述 主要优点 主要缺点 JNI实现的简单例子 开发工具 简略步骤 1,在eclipse的 'java类' 中声明一个 'native方法'; 2,使用 'javah' 命令生成包含'nativ ...

  4. Tomcat6.0数据源配置

    涉及context.xml和server.xml http://blog.csdn.net/onlymilan/article/details/5493485

  5. Win7启动修复MBR(Win7+Linux删除Linux后进入grub rescue的情况)

    事因:我的笔记本原先同时安装了Win7+Linux,昨天发现硬盘实在不够用(才60G,虽然还有个500G的移动硬盘),就想把里面的Ubuntu格了.都是用虚拟机做测试的多.后来就格了Ubuntu所在的 ...

  6. oracle 语句

    1.查询TRENDCHART_DLT表中的30条数据,统计字段FRONT01='0',BACK12='0'的条数 select sum(case when FRONT01='0' then 1 els ...

  7. 你好,C++(25)函数调用和它背后的故事5.1.2 函数调用机制

    5.1.2  函数调用机制 在前面的学习中,我们多次提到了“调用函数”的概念.所谓调用函数,就是将程序的执行控制权从调用者(某个函数)交给被调用的函数,同时通过参数向被调用的函数传递数据,然后程序进入 ...

  8. URAL 1008 - Image Encoding(bfs坑爹题)

    坑爹题,两种输入输出互相交换,裸bfs #include <stdio.h> #include <string.h> typedef struct { int x; int y ...

  9. 常用CSS代码片断

    单行文本截字 .nowrap { overflow: hidden; white-space: nowrap; text-overflow: ellipsis; word-break: normal; ...

  10. WordPress社会化评论插件多说、有言、灯鹭、评论啦

    社会化登录是指网站的用户通过使用QQ.人人网.腾讯微博.新浪微博等社会化媒体账号登录该网站,并让用户能够使用社会化媒体账号进行允许的操作,如评论.分享等.经过简单的代码整合,社会化媒体账号可以在第三方 ...