其他分享
首页 > 其他分享> > HDU 1547 Bubble Shooter dfs

HDU 1547 Bubble Shooter dfs

作者:互联网

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
 
int n, m, x, y, ans;
char map[120][120]; 
bool visit[120][120];
//六个方向,且奇书偶数行是不一样的 
void dfs1(int a, int b, char c)  //将与射出去的泡泡相连的相同的泡泡全都标记 
{
	if(a == 0 || b == 0 || map[a][b] != c || visit[a][b] == 1) return;
	visit[a][b] = 1;
	if(a%2 == 1)
		{
			dfs1(a, b-1, c);
			dfs1(a, b+1, c);
			dfs1(a+1, b, c);
			dfs1(a+1, b-1, c);
			dfs1(a-1, b, c);
			dfs1(a-1, b-1, c);
		}
	else
		{
			dfs1(a, b-1, c);
			dfs1(a, b+1, c);
			dfs1(a+1, b, c);
			dfs1(a+1, b+1, c);
			dfs1(a-1, b, c);
			dfs1(a-1, b+1, c); 
		}
}
//从上往下把与首行直接或间接相连的泡泡标记 
void dfs2(int a, int b)
{
	if(map[a][b] == 'E' || b == 0 || a == 0 || visit[a][b] == 1) return;
	visit[a][b] = 1;
	if(a%2 == 1)
	{
		dfs2(a-1, b);
		dfs2(a-1, b-1);
		dfs2(a+1, b);
		dfs2(a+1, b-1);
		dfs2(a, b-1);
		dfs2(a, b+1);
	}
	else
	{
		dfs2(a-1, b);
		dfs2(a-1, b+1);
		dfs2(a+1, b);
		dfs2(a+1, b+1);
		dfs2(a, b-1);
		dfs2(a, b+1);
	}
}

int main()
{
	while(~scanf("%d %d %d %d", &n, &m, &x, &y))
	{
		ans = 0;
		memset(visit, 0, sizeof visit);
		for(int i = 0; i < 120; i++)
			for(int j = 0; j < 120; j++)
				map[i][j] = 'E';
				
		for(int i = 1; i <= n; i++)
		{
			if(i%2 == 1)
				for(int j = 1; j <= m; j++)
					scanf(" %c", &map[i][j]);
			else
				for(int j = 1; j <= m-1; j++)
					scanf(" %c", &map[i][j]);
		}
		
		dfs1(x, y, map[x][y]);
		for(int i = 1; i <= n; i++)
			for(int j = 1; j <= m; j++)
				if(visit[i][j] == 1)
					ans++;
		
		if(ans >= 3)
		{
			for(int j = 1; j <= m; j++)
				dfs2(1, j);
				
			for(int i = 1; i <= n; i++)
				for(int j = 1; j <= m; j++)
					if(map[i][j] != 'E' && visit[i][j] == 0) 
					{
						ans++;
					}
			printf("%d\n", ans);
		}
		else printf("0\n");
	}
	return 0;
}

标签:1547,HDU,map,int,Shooter,visit,120,dfs2,dfs1
来源: https://www.cnblogs.com/znk97/p/14345439.html