Bubble Shooter
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 3 Accepted Submission(s) : 3
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
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.
![](https://i-blog.csdnimg.cn/blog_migrate/b64e26ed06f0523130a2b8899312a7dd.jpeg)
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).
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
——————————————————————————————————————————————————————————————
其实就是泡泡龙的游戏,给你起始的地图,以及刚打出去的泡泡的位置,如果与刚打出的泡泡相连的泡泡数大于等于3,则相连的相同颜色的泡泡会掉下来,之后,没有与顶层泡泡直接或间接相连的泡泡也会掉下来。问掉下来的泡泡总数。
分析:其实就是模拟一下就可以了。首先将与起始点直接或间接相连的相同颜色的泡泡标记一下,看总数ans是否大于等于3.
奇数层偶数层分开讨论
之后要分俩种情况讨论了:
1) ans<3 。那么要将之前的标记清除,找出与顶层泡泡直接相连或间接相连的泡泡总数lt,sum-lt就是答案了。这里解决了一个特殊情况,本来以为ans<3的话,直接输出0就可以了,但其实很有可能,即使ans<3,但起始的地图也会有一些泡泡会掉下来。
2)ans>=3,这种情况下,就还是找出与顶层直接相连或间接相连的泡泡总数lt,直接sum-lt。这里就将俩种情况统一起来了。
分析:其实就是模拟一下就可以了。首先将与起始点直接或间接相连的相同颜色的泡泡标记一下,看总数ans是否大于等于3.
奇数层偶数层分开讨论
之后要分俩种情况讨论了:
1) ans<3 。那么要将之前的标记清除,找出与顶层泡泡直接相连或间接相连的泡泡总数lt,sum-lt就是答案了。这里解决了一个特殊情况,本来以为ans<3的话,直接输出0就可以了,但其实很有可能,即使ans<3,但起始的地图也会有一些泡泡会掉下来。
2)ans>=3,这种情况下,就还是找出与顶层直接相连或间接相连的泡泡总数lt,直接sum-lt。这里就将俩种情况统一起来了。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#include<cmath>
using namespace std;
int dir1[6][2]= {{-1,0},{-1,-1},{1,0},{1,-1},{0,-1},{0,1}}; //偶
int dir2[6][2]= {{-1,0},{-1,1},{1,0},{1,1},{0,-1},{0,1}}; //奇
char mp[105][105];
int m,n,ans,lt;
char s;
bool cheak(int x,int y)
{
if(x<0||x>=m||y<0||y>=n||mp[x][y]=='E')
return 0;
return 1;
}
void dfs(int x,int y)
{
int xx,yy;
ans++;
mp[x][y]='E';
if(x%2==0)
{
for(int i=0; i<6; i++)
{
xx=x+dir1[i][0];
yy=y+dir1[i][1];
if(cheak(xx,yy)&&mp[xx][yy]==s)
dfs(xx,yy);
}
}
else
{
for(int i=0; i<6; i++)
{
xx=x+dir2[i][0];
yy=y+dir2[i][1];
if(cheak(xx,yy)&&mp[xx][yy]==s)
dfs(xx,yy);
}
}
}
void dfs2(int x,int y)
{
int xx,yy;
lt++;
mp[x][y]='E';
if(x%2==0)
{
for(int i=0; i<6; i++)
{
xx=x+dir1[i][0];
yy=y+dir1[i][1];
if(cheak(xx,yy))
dfs2(xx,yy);
}
}
else
{
for(int i=0; i<6; i++)
{
xx=x+dir2[i][0];
yy=y+dir2[i][1];
if(cheak(xx,yy))
dfs2(xx,yy);
}
}
}
int main()
{
int h,w;
while(~scanf("%d%d%d%d",&m,&n,&w,&h))
{
int sum=0;
for(int i=0; i<m; i++)
{
scanf("%s",mp[i]);
for(int j=0;j<strlen(mp[i]);j++)
{
if(mp[i][j]!='E')
sum++;
}
}
for(int i=1; i<m; i+=2)
{
mp[i][n-1]='E';
}
ans=0;
s=mp[w-1][h-1];
dfs(w-1,h-1);
if(ans<3)
printf("0\n");
else
{
lt=0;
for(int i=0; i<n; i++)
{
if(mp[0][i]!='E')
dfs2(0,i);
}
printf("%d\n",sum-lt);
}
}
return 0;
}