首页 > 搜索 > DFS搜索 > HDU 1547 Bubble Shooter-DFS-[解题报告] C++
2013
12-12

HDU 1547 Bubble Shooter-DFS-[解题报告] C++

Bubble Shooter

问题描述 :

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.

输入:

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).

输出:

For each test case, output an integer indicating how many bubbles will explode.

样例输入:

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

样例输出:

3
8
3
0

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

 题意: 给你个状态(起初不会有悬挂点,a-z为泡泡 E为空白) 问会有多少个泡泡掉下来(与起点相连个数大于3 或者 未于顶点相连就会掉下来 )

注意奇偶方向

#include "stdio.h"
#include "string.h"
const int maxn = 105;
int h,w,sx,sy,ans;
int xs[][6] = { {0,-1,-1,0,1,1},{0,-1,-1,0,1,1}};
int ys[][6] = { {1,1,0,-1,0,1},{1,0,-1,-1,-1,0}};
char map[maxn][maxn];
int DFS( int x,int y,char ch )
{
	int sum = 1;
	map[x][y] = 'E';
	for( int i = 0; i < 6; i ++ )
	{
		int xx = x + xs[x%2][i];
		int yy = y + ys[x%2][i];
		if( map[xx][yy] == ch )
		{
			sum += DFS(xx,yy,ch);
		}
	}
	return sum;
}
void dfs( int x,int y )
{
	map[x][y] = 'E';
	for( int i = 0; i < 6; i ++ )
	{
		int xx = x + xs[x%2][i];
		int yy = y + ys[x%2][i];
		if( map[xx][yy] != 'E' )
		{
			dfs(xx,yy);
		}
	}
}
int main()
{
	//freopen("data.txt","r",stdin);
	int k;
	while( scanf("%d%d%d%d",&h,&w,&sx,&sy) == 4 )
	{
		memset( map,'E',sizeof(map) );
		for( int i = 1; i <= h; i ++ )
		{
			getchar();
			if( i%2 )	k = w;
			else k = w-1;
			for( int j = 1; j <= k; j ++ )
			{
				scanf("%c",&map[i][j]);
			}
		}
		ans = DFS(sx,sy,map[sx][sy]);          //从起点标记
		if( ans >= 3 )
		{
			for( int i = 1; i <= w; i ++ )		//从顶行标记
			{
				if( map[1][i] != 'E' )
					dfs(1,i);
			}
			for( int i = 2; i <= h; i ++ )
			{
				for( int j = 1; j <= w; j ++ )
				{
					if( map[i][j] != 'E' )
						ans += DFS(i,j,map[i][j]);
				}
			}
		}
		else
			ans = 0;
		printf("%d\n",ans);
	}
	return 0;
}
/*
4 5 4 4
abbbb
abbb
aabcb
aaab
*/

 

解题报告转自:http://blog.csdn.net/u011742541/article/details/16810503


,
  1. int half(int *array,int len,int key)
    {
    int l=0,r=len;
    while(l<r)
    {
    int m=(l+r)>>1;
    if(key>array )l=m+1;
    else if(key<array )r=m;
    else return m;
    }
    return -1;
    }
    这种就能避免一些Bug
    l,m,r
    左边是l,m;右边就是m+1,r;

  2. simple, however efficient. A lot of instances it is difficult to get that a??perfect balancea?? among usability and appearance. I must say that youa??ve done a exceptional task with this. Also, the blog masses quite fast for me on Web explore.