HDU1547 Bubble Shooter(DFS)

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

主要思想:

两个深搜:

第一个是寻找能靠相连爆炸的泡泡的数量

第二个是寻找爆炸后不和第一行泡泡直接或间接相连的泡泡数量

在这个问题上,让我比较难理解的是奇数行和偶数行的泡泡相连方向的问题,附个图:

其他的理解起来就比较简单了

 

import java.util.Arrays;
import java.util.Scanner;

public class Main {
    static char[][] a=new char[105][105];
    static boolean[][] b=new boolean[105][105];
    static int H,W,h,w;
    static int ans;
    static int dir1[][]={{0,1},{0,-1},{1,0},{-1,0},{-1,-1},{1,-1}};//偶数行0 2 4对于数组
    static int dir2[][]={{0,1},{0,-1},{1,0},{-1,0},{-1,1},{1,1}};//奇数行1 3 5这两个数组是重点
    static void dfs1(int x,int y){
        int a1,b1;
        for(int i=0;i<6;i++){
            if(x%2==1){
                a1=x+dir2[i][0];
                b1=y+dir2[i][1];
            }else{
                a1=x+dir1[i][0];
                b1=y+dir1[i][1];
            }
            if(a1<0||a1>=H||b1<0||(a1%2==1&&b1>=W-1)||b1>=W) continue;//是否超界
            if(a[a1][b1]!=a[h-1][w-1]) continue;//是否相连
            if(b[a1][b1]==true) continue;//是否标记
            b[a1][b1]=true;
            ans++;
            dfs1(a1,b1);                        
        }
    }
    static void dfs2(int x,int y){
        int a1,b1;
        for(int i=0;i<6;i++){
            if(x%2==1){
                a1=x+dir2[i][0];
                b1=y+dir2[i][1];
            }else{
                a1=x+dir1[i][0];
                b1=y+dir1[i][1];
            }
            if(a1<0||a1>=H||b1<0||(a1%2==1&&b1>=W-1)||b1>=W) continue;//是否超界
            if(a[a1][b1]=='E') continue;//是否为空 
            //if(a[a1][b1]!=a[x][y]) continue;//是否相连
            if(b[a1][b1]==true) continue;//是否标记
            b[a1][b1]=true;
            //ans++;
            dfs2(a1,b1);                        
        }
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner sc=new Scanner(System.in);
        while(sc.hasNext()){
            for(int i=0;i<105;i++){
                Arrays.fill(a[i],' ');
                Arrays.fill(b[i],false);
            }
            ans=0;
            H=sc.nextInt();
            W=sc.nextInt();
            h=sc.nextInt();
            w=sc.nextInt();
            for(int i=0;i<H;i++){
                String s=sc.next();
                for(int j=0;j<s.length();j++){
                    a[i][j]=s.charAt(j);
                }
            }
            b[h-1][w-1]=true;
            ans=1;
            dfs1(h-1,w-1);
            if(ans<3){                
                ans=0;
                for(int i=0;i<105;i++)
                    Arrays.fill(b[i],false);
            }//当不能依靠相连爆炸时
            
            for(int i=0;i<W;i++){//把与第一行直接或间接相连的泡泡标记
                if(b[0][i]==false&&a[0][i]!='E'&&a[0][i]!=' '){
                    b[0][i]=true;
                    dfs2(0,i);
                }
            }
            for(int i=0;i<H;i++)//没标记泡泡也就是没和第一行相连的将会爆炸再记一次数量
                for(int j=0;j<W;j++)
                    if(b[i][j]==false&&a[i][j]!='E'&&a[i][j]!=' ')
                        ans++;
            System.out.println(ans);
        }        
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值