欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Lake Counting

程序员文章站 2022-07-14 20:50:57
...
#include "stdafx.h"
#include<iostream>
#define MAX 500
using namespace std;
char imap[MAX][MAX];
int n, m;
void dfs(int x, int y)
{
	imap[x][y] = '.';
	for (int dx = -1; dx <= 1; dx++)
		for (int dy = -1; dy <= 1; dy++)
		{
			int nx = x + dx;
			int ny = y + dy;
			if (nx >= 0 && nx <= n&&ny >= 0 && ny <= m&&imap[nx][ny] == 'W')
				dfs(nx, ny);
		}
}
int solve()
{
	int res = 0;
	for (int i = 0; i < n; i++)
		for (int j = 0; j < m;j++)
			if (imap[i][j] == 'W')
			{
				dfs(i, j);
				res++;
			}
	return res;
}
int main()
{
	while (cin >> n >> m)
	{
		for (int i = 0; i < n; i++)
			for (int j = 0; j < m; j++)
				cin >> imap[i][j];
		cout << solve() << endl;
	}
}

版权声明:本文为博主原创文章,未经博主允许不得转载。