现在的位置: 首页 > 综合 > 正文

HDU 1547 Bubble Shooter BFS

2018年01月20日 ⁄ 综合 ⁄ 共 3056字 ⁄ 字号 评论关闭

Bubble Shooter

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

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
/*
HDOJ 1547 BFS 泡泡龙游戏 
题目大意就是:输入n行m列,和发出小球的到达的位置h,w,
用a~z表示不同颜色的小球,E表示该位置为空,
奇数行有m个小球位置,偶数行有m-1个小球位置
求发射出该小球后掉落的小球数量有多少,
已知相同颜色的小球连通数量>=3就会掉落和悬空的小球也会掉落(即周围没有小球与它相连)

所以:
1:先从发射出的小球那开始广搜,判断相同颜色连通的数量是否>=3,
是则将这些位置标记为空,为掉落的。 
2:接着判断有无悬空的;
对第一行小球位置开始深搜,所有搜索到的小球都不是悬空的,即不会掉落,
没搜索到的会掉落,包括已经标记的都是掉落的。
(这里广搜也一样,反正第一行的都要搜,已经搜到的标记即可) 
*/

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;

const int MAX=101*101;
char map[101][101];
int n,m,s[MAX];

int dir0[6][2]={0,1,0,-1,1,0,-1,0,-1,-1,1,-1};//偶数行;根据输入的数字可看出 
int dir1[6][2]={0,1,0,-1,1,0,-1,0,-1,1,1,1};//奇数行


int BFS(int f,int xx,int yy)
{
    
    char target=map[xx][yy];
    int start,sum,x,y,a,b,i;
    queue<int> que;
    
    map[xx][yy]='E';
    que.push(xx*m+yy);//每个点的位置表示成一维的
    
    sum=0;
    while(!que.empty())
    {
        start=que.front();
        que.pop();
        
        if(f) s[sum]=start;
        sum++;
        
        x=start/m;y=start%m;
        
        for(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 || a>=n || (a%2 && b>=m-1) || b<0 || b>=m)continue;  
            if(map[a][b] == 'E')continue;//表示该点为空  
            if(f && map[a][b] != target)continue;//查找周围连通且相同颜色的球 
            
            map[a][b]='E';
            que.push(a*m+b);
        }    
    }
    
    if(f&&sum<3)
        {    
            for(i=0;i<sum;i++)
                {
                map[s[i]/m][s[i]%m]=target;
                }
        }
    
    return sum; 
} 

int main()
{
    
    int x,y,i,j,sum,ans;
    while(scanf("%d%d%d%d",&n,&m,&x,&y)!=EOF)
    {
        x--;y--;//位置坐标因为数组从0开始所以减1 
        for(i=0;i<n;i++)
            scanf("%s",map[i]);
        sum=0;ans=0; 
        for(i=0;i<n;i++)
            for(j=0;j<m;j++)
                if(map[i][j]>='a'&&map[i][j]<='z')
                    sum++;
        
        BFS(1,x,y);//BFS(0/1) 两种;1:搜颜色相同点 2:查找与顶端相邻的点 
    
        for(i=0;i<m;i++) //查询与顶端相邻的点 
            if(map[0][i]!='E')
                ans+=BFS(0,0,i);
        printf("%d\n",sum-ans); 
        
    }
    return 0;
}

抱歉!评论已关闭.