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

【DFS】 hdu1547 Bubble Shooter

2018年01月14日 ⁄ 综合 ⁄ 共 2615字 ⁄ 字号 评论关闭

Bubble Shooter

http://acm.hdu.edu.cn/showproblem.php?pid=1547



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

题意:给你一个局面和告诉你最新加入的点,问此时能消去多少点。可消去的点由与新加入的点的相同颜色且相连个数大于等于三的点块和消除之后与顶端不相连的点块两部分组成。(有点解释不清楚,详情见题目)

题解:先由新加入点开始深搜,计算出相连且颜色相同的点块中点的个数,同时经过的点置0。如果点个数小于3则输出0,反之再枚举顶端的点,从每个点出发深搜(不能经过置0的点),最后图中剩下没有置0的点就是会掉下去的点,计数即可。

#include<cstdio>
#include<cstring>
using namespace std;
int dir[6][2]={-1,1,0,2,1,1,1,-1,0,-2,-1,-1};
char mat[205][205];
int n,m,x,y,maxx;
void dfs(int a,int b,char c)
{
    int p,q;
    maxx++;
    mat[a][b]=0;
    for(int i=0;i<6;++i)
    {
        p=a+dir[i][0];q=b+dir[i][1];
        if(0<=a&&a<=n&&0<=b&&b<=m&&mat[p][q])
        {
            if(mat[p][q]==c)
               dfs(p,q,c);
            if(c=='0')
               dfs(p,q,c);
        }
    }
}
int main()
{
    int ans;
    for(;~scanf("%d%d%d%d",&n,&m,&x,&y);)
    {
        memset(mat,0,sizeof(mat));
        ans=maxx=0;
        for(int i=1;i<=n;++i)
            scanf("%s",mat[i]);
        for(int i=1;i<=n;i+=2)
            for(int j=m-1;j>=0;--j)
            {
                if(mat[i][j]=='E')  mat[i][j]=0;
                mat[i][j*2+1]=mat[i][j];
                mat[i][j]=0;
            }
        for(int i=2;i<=n;i+=2)
            for(int j=m-2;j>=0;--j)
            {
                if(mat[i][j]=='E')  mat[i][j]=0;
                mat[i][j*2+2]=mat[i][j];
                mat[i][j]=0;
            }
        m=2*m;
        if(x&1) y=(y-1)*2+1;
        else    y=(y-1)*2+2;
        dfs(x,y,mat[x][y]);
        ans=maxx;
        if(ans<3)
            ans=0;
        else
        {
            for(int i=1;i<=m;++i)
                if(mat[1][i])
                    dfs(1,i,'0');
            for(int i=1;i<=n;++i)
                for(int j=1;j<=m;++j)
                   if(mat[i][j]) ans++;
        }
        printf("%d\n",ans);
    }
    return 0;
}

抱歉!评论已关闭.