Lake Couting_poj2386_p32

#include
#include
using namespace std;
#include

#define MAX 100
int N, M;
char field[MAX][MAX];

void dfs(int x, int y)
{
	field[x][y] = '.';
	for (int dx = -1; dx <= 1; dx++)
	{
		for (int dy = -1; dy <= 1; dy++)
		{
			int nx = x + dx, ny = y + dy;
			if (nx >= 0 && nx < N &&
				ny >= 0 && ny < M && field[nx][ny] == 'W')
				dfs(nx, ny);
		}
	}
	return;
}

int main()
{
	while (~scanf("%d %d", &N, &M))
	{
		int res = 0;
		memset(field, '0', sizeof(field));
		for (int i = 0; i < N; i++)
		{
			for (int j = 0; j < M; j++)
			{
				cin >> field[i][j];
			}
		}

		for (int i = 0; i < N; i++)
		{
			for (int j = 0; j < M; j++)
			{
				if (field[i][j] == 'W')
				{
					dfs(i, j);
					res++;
				}
			}
		}
		printf("%d\n", res);
	}
	
	//getchar();
	return 0;
}

 

你可能感兴趣的:(挑战程序设计竞赛)