HDU1547

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

思路:

我们先DFS一下这个点是否有和他相同的相连的,

1.如果小于3  那么直接输出0

2.大于等于3 这些东西都被标记成E 然后再从第一行开始找到和他们相连的那么就是剩下的相连的有多少

然后总数-相连=====   >大于等于3 + 单独的 有点反向思维的感觉

代码:

import java.util.Scanner;
import java.util.Arrays;
public class Hello {
	static Scanner sc = new Scanner(System.in);
	static int h,w,sx,sy;
	static final int N = 105;
	static char maze[][]= new char [N][N];
	static int dirodd[][] = {{0,1},{0,-1},{1,0},{-1,0},{-1,-1},{1,-1}};
	static int direven[][] = {{0,1},{0,-1},{1,0},{-1,0},{1,1},{-1,1}};
	static int cnt = 0;
	static char ch;
	static void DFS(int x,int y,int flag)
	{
		for(int i = 0; i < 6; i++)
		{
			int fx,fy;
			if(x % 2 == 1)
			{
				fx = x + dirodd[i][0];
				fy = y + dirodd[i][1];
			}else 
			{
				fx = x + direven[i][0];
				fy = y + direven[i][1];
			}
			if(fx < 1 || fy < 1 || fx > h || fy > w || maze[fx][fy] == 'E')
				continue;
			if(flag == 1) {
				if(maze[fx][fy] == ch) {
					cnt++;
					maze[fx][fy] = 'E';
					DFS(fx,fy,flag);
				}
			}else 
			{
				cnt++;
				maze[fx][fy] = 'E';
				DFS(fx,fy,flag);
			}
		}
		return ;
	}
	public static void main(String[] args) {
		while(sc.hasNext())
		{
			h = sc.nextInt();
			w = sc.nextInt();
			sx = sc.nextInt();
			sy = sc.nextInt();
			for(int i = 1; i <= 101; i++)
				for(int j = 1; j <= 101; j++)
					maze[i][j] = 'E';//先表示所有地方都是空的
			int sum = 0;
			for(int i = 1; i<= h; i++)
			{
				int k = w;
				if(i % 2 == 0) k--;
				String s;
				s = sc.next();
				for(int j = 1; j <= k; j++)
				{
					maze[i][j] = s.charAt(j - 1);
					if(maze[i][j] >= 'a' && maze[i][j] <='z')
						sum++; //表示有多少个
				}
			}
			cnt = 1;
			ch = maze[sx][sy];
			maze[sx][sy] = 'E';
			DFS(sx,sy,1);//应该说一旦找就是找到所有与它相连的然后设为没有
			if(cnt < 3) //表示不大于2个和他相等 所以说...直接输出0 因为一开始就是联着的
				System.out.println(0);
			else 
			{
				cnt = 0;
				for(int j = 1 ; j <= w; j++)
				{
					if(maze[1][j] != 'E')
					{
						cnt ++;
						maze[1][j] = 'E';
						DFS(1,j,0);
					}
				}
				System.out.println(sum - cnt);
			}
		}
	}
}

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值