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. Redux的State不应该全部放在Store里

    使用了redux管理应用的状态,应用的状态不应该全部放在Store里面. 前端状态主要有一下两种: 1. Domain data 2. UI State 1. Domain data 来自于服务端对领 ...

  2. 用CSS3实现的addidas阿迪达斯标志LOGO

    <!DOCTYPE html> <html> <head> <meta charset=utf-8 /> <title>用CSS3实现的ad ...

  3. VS 2013 with update安装失败(kb2829760)解决方案

    update过程中遇到kb2829760补丁无法更新而导致vs安装失败的解决方法: 1.安装KB2829760: 2.安装KB2829760中文语言包: 3.安装VS2013 with update. ...

  4. Codeforces Round #344 (Div. 2) B

    B. Print Check time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  5. bzoj 1520 [POI2006]Szk-Schools 费用流

    [POI2006]Szk-Schools Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 743  Solved: 381[Submit][Status][ ...

  6. linux查找进程id端口占用和杀死进程

    linux 查找进程id端口占用和杀死进程 ps 命令用于查看当前正在运行的进程 辅助上grep 用于搜索匹配ps -ef | grep java ps ax : 显示当前系统进程的列表 ps aux ...

  7. js数组的误解

    js数组实际是个残废货,没有关联数组这一说,要实现真正意义上的关联数组只能用对象,那你肯定不服气了,说怎么没有关联数组,我来给你写一个: var arr = []; arr['a'] = 1; arr ...

  8. 函数式编程--响应式编程 ---android应用例子

    RxJava implements this operator as create. It is good practice to check the observer’s isUnsubscribe ...

  9. centos 挂载数据盘

    第一.检查硬盘设备是否有数据盘 fdisk -l 第二.数据硬盘分区 fdisk /dev/vdb 第三.ext3格式化分区 mkfs.ext3 /dev/vdb1 第四.挂载新分区 A - 新建目录 ...

  10. POJ 2431 Expedition (优先队列+贪心)

    题目链接 Description A group of cows grabbed a truck and ventured on an expedition deep into the jungle. ...