【ZOJ2743】Bubble Shooter(dfs)

题目链接

Bubble Shooter


Time Limit: 2 Seconds      Memory Limit: 65536 KB


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

参考http://www.cnblogs.com/nanke/archive/2011/12/07/2279210.html

【题意】

泡泡龙游戏,给你起始的地图,以及刚打出去的泡泡的位置,如果与刚打出的泡泡相连的泡泡数大于等于3,则相连的相同颜色的泡泡会掉下来,之后,没有与顶层泡泡直接或间接相连的泡泡也会掉下来。问掉下来的泡泡总数。

【解题思路】

其实就是模拟一下就可以了。首先将与起始点直接或间接相连的相同颜色的泡泡标记一下,看总数num是否大于等于3.,sum表示起始时的泡泡总数。

之后要分俩种情况讨论了:

1) num<3 。那么要将之前的标记清除,找出与顶层泡泡直接相连或间接相连的泡泡总数num,sum-num就是答案了。这里解决了一个特殊情况,本来以为num<3的话,直接输出0就可以了,但其实很有可能,即使num<3,但起始的地图也会有一些泡泡会掉下来。

2)num>=3,这种情况下,不需要清楚标记,因为相同颜色的已经被消去了,就还是找出与顶层直接相连或间接相连的泡泡总数num,直接sum-num。这里就将俩种情况统一起来了。

【代码】

#include<bits/stdc++.h>
using namespace std;
const int maxn=105;
char p[maxn][maxn];
int n,m,vis[maxn][maxn],xx,yy,num;
int dir1[6][2]={{0,1},{0,-1},{1,0},{-1,0},{-1,-1},{1,-1}};//偶数行
int dir2[6][2]={{0,1},{0,-1},{1,0},{-1,0},{-1,1},{1,1}};//奇数行
void dfs(int x,int y,int flag)
{
    if(x%2)
    {
        for(int i=0;i<6;i++)
        {
            int x1=x+dir2[i][0];
            int y1=y+dir2[i][1];
            if(x1<0 || x1>=n || y1<0 ||y1>=m || vis[x1][y1] || p[x1][y1]=='E')
                continue;
            if(flag)
            {
                if(p[x1][y1]!=p[xx][yy])
                    continue;
            }
            vis[x1][y1]=1;
            num++;
            dfs(x1,y1,flag);
        }
    }
    else
    {
        for(int i=0;i<6;i++)
        {
            int x1=x+dir1[i][0];
            int y1=y+dir1[i][1];
            if(x1<0 ||x1>=n ||y1<0 ||y1>=m|| vis[x1][y1] || p[x1][y1]=='E')
                continue;
            if(flag)
            {
                if(p[x1][y1]!=p[xx][yy])
                    continue;
            }
            vis[x1][y1]=1;
            num++;
            dfs(x1,y1,flag);
        }
    }
}
int main()
{
    while(~scanf("%d%d%d%d",&n,&m,&xx,&yy))
    {
        int sum=0;
        for(int i=0;i<n;i++)
        {
            scanf("%s",p[i]);
            if(i%2)
            {
                p[i][m-1]='E';
                p[i][m]='\0';
            }
            for(int j=0;j<m;j++)
                if(p[i][j]!='E')
                    sum++;
        }
        xx--;yy--;
        num=1;
        memset(vis,0,sizeof(vis));
        vis[xx][yy]=1;
        dfs(xx,yy,1);
        if(num<3)
            memset(vis,0,sizeof(vis));
        num=0;
        for(int i=0;i<m;i++)
        {
            if(!vis[0][i] && p[0][i]!='E')
            {
                num++;
                vis[0][i]=1;
                dfs(0,i,0);
            }

        }
        printf("%d\n",sum-num);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_39826163/article/details/81837541