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

题意:

 输入:

  将有许多组测试数据。每组测试将以四个整数开始:H(区域的高度,2 <= H <= 100),W(区域的宽度,2 <= W <= 100,在上面的图片中,W是10),h(最近发射上去的泡泡的竖直位置,计数方式从顶部至底部,最顶部为第1行),w(最近发射上去的泡泡的水平位置,计数方式从左至右,最左边为第1列)。

  接下来后面跟着H行,奇数行包含W个字符,偶数行包含W-1个字符(参考上面的图片)。每个字符要么是‘a’到‘z’中的一个小写字母,表明在这个位置上泡泡的颜色,要么是一个字母‘E’,表明一个空位置。你可以认为给出的测试例子符合实际游戏情况,没有bug(所有的泡泡直接或者间接与最顶上的一行的泡泡相连,最近发射上去的泡泡的位置不为空)。

输出:

  对于每个测试,输出一个整数,表明有多少泡泡会爆炸。

思路:

       输入的时候就可以求出泡泡总数sum;再求出颜色相同的要爆炸的气球总数ans1;最后求出第一行及与第一行相连的未标记的泡泡总数ans2;

      如果ans1<3,输出0; 否则输出sum-ans2

 需要注意的地方:

          1.深搜是6个方向,而且奇数行和偶数行不一样

          2.深搜的时候判断yy的时候,奇数行和偶数行也是不一样的长度,yy>W-(xx+1)%2,偶数减1,奇数减0

          3.写java代码需要注意的一些问题--二维字符数组map[i].length是指自己开辟的数组的总长度,而不是实际拥有的元素的长度

          把一个二维字符数组转化成下标从1开始的数组--map[i][j]=s.charAt(j-1),别忘了减1,因为字符串是从下标0开始的
  

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

public class Main{ 
	static int H,W,h,w;
	static int sum,ans1,ans2;
	static final int max=105;
	static char map[][]=new char[max][max];
	static boolean vis[][]=new boolean[max][max];
	static int dirx1[]={0,0,1,-1,1,-1};//偶数
	static int diry1[]={1,-1,0,0,1,1};
	static int dirx2[]={0,0,1,-1,1,-1};//奇数
	static int diry2[]={1,-1,0,0,-1,-1};
	public static void dfs1(int x,int y){
		for(int i=0;i<6;i++){
			int xx=0,yy=0;
			if(x%2==0){
				xx=x+dirx1[i]; yy=y+diry1[i];
			}
			else{
				xx=x+dirx2[i]; yy=y+diry2[i];
			}
			if(xx<1||xx>H||yy<1||yy>W-(xx+1)%2) continue;
			if(vis[xx][yy]||map[xx][yy]!=map[x][y]) continue;
			vis[xx][yy]=true; ans1++;
			dfs1(xx,yy);
		}
	}
	public static void dfs2(int x,int y){
		for(int i=0;i<6;i++){
			int xx=0,yy=0;
			if(x%2==0){
				xx=x+dirx1[i]; yy=y+diry1[i];
			}
			else{
				xx=x+dirx2[i]; yy=y+diry2[i];
			}
			if(xx<1||xx>H||yy<1||yy>W-(xx+1)%2) continue;
			if(vis[xx][yy]) continue;
			vis[xx][yy]=true; ans2++;
			dfs2(xx,yy);
		}
	}
    public static void main(String[] args) {
	   Scanner scan=new Scanner(System.in);
	   while(scan.hasNext()){
		   sum=0;
		   ans1=0;
		   ans2=0;
		   H=scan.nextInt();
		   W=scan.nextInt();
		   h=scan.nextInt();
		   w=scan.nextInt();
		   for(int i=0;i<max;i++) Arrays.fill(vis[i], false);
		   for(int i=1;i<=H;i++){
			   String s=scan.next();//map[i]指的是你一开始开辟的总长度,如果想求实际长度,换方法
			   for(int j=1;j<=s.length();j++){
				   map[i][j]=s.charAt(j-1);//字符串s是从0开始的,所以一定要j-1
				   if(map[i][j]!='E') sum++;  
				   if(map[i][j]=='E') vis[i][j]=true;// 标记空格
		    }
		   }
		   //第一次先标记出颜色相同的要爆炸的泡泡的数目
		   ans1=1;
		   vis[h][w]=true;
		   dfs1(h,w);
		   //第二次求出第一行及与第一行相连的泡泡未被标记的总数
		   for(int i=1;i<=W;i++){
			   if(!vis[1][i]){//没有被标记且不是空格
				   ans2++;
				   vis[1][i]=true;
				   dfs2(1,i);
			   }
			   
		   }
		   //System.out.println(ans1);
		   if(ans1<3){
			   System.out.println("0");
		   }
		   else{
			   System.out.println(sum-ans2);
		   }
	   }
}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小鱼爱吃火锅

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值