输入样例:
10 12
W…WW.
.WWW…WWW
…WW…WW.
…WW.
…W…
…W…W…
.W.W…WW.
W.W.W…W.
.W.W…W.
…W…W.
输出样例:
3
AC代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
typedef pair<int, int> PII;
const int N = 1010,M=N*N;
int n, m;
char map[N][N];
PII q[M];
bool st[N][N];
void bfs(int sx, int sy) {
int hh = 0, tt = 0;
q[0] = { sx,sy };
st[sx][sy] = true;
while (hh <= tt) {
PII t = q[hh++];
for (int i = t.first - 1; i <= t.first + 1; i++) {
for (int j = t.second - 1; j <= t.second + 1; j++) {
if (st[i][j] || map[i][j] == '.') continue;
if (i < 0 || i >= n || j < 0 || j >= m) continue;
if (i == t.first&&j == t.second) continue;
q[++tt] = { i,j };
st[i][j] = true;
}
}
}
}
int main()
{
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i++) scanf("%s", map[i]);
int cnt = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
if (map[i][j] == 'W' && !st[i][j]) {
bfs(i, j);
cnt++;
}
printf("%d", cnt);
system("pause");
return 0;
}