P1596 [USACO10OCT]Lake Counting S

#include 
using namespace std;
int n, m;
char p[101][101];

int dx[] = {1, -1, 0, 0, 1, -1, -1, 1};
int dy[] = {0, 0, 1, -1, 1, -1, 1, -1};
queue>q;
int vis[101][101];
int ct = 0 ;
void bfs(int x, int y) {

	q.push({x, y});
	vis[x][y] = 1;
	while (!q.empty()) {
		auto u = q.front();
		q.pop();
		for (int i = 0; i < 8; i++) {
			int xx = u.first + dx[i];
			int yy = u.second + dy[i];
			if (xx >= 0 && xx <= n - 1 && yy >= 0 && yy <= m - 1 && p[xx][yy] == 'W' && !vis[xx][yy]) {
				q.push({xx, yy});
				vis[xx][yy] = 1;
			}
		}
	}
}

int main() {
	cin >> n >> m;
	for (int i = 0; i < n; i++) {
		scanf("%s", p + i);
	}

	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			if (p[i][j] == 'W' && !vis[i][j]) {
				bfs(i, j);
				ct++;
			}
		}
	}
	cout << ct;



}

你可能感兴趣的:(c++,开发语言)