//160K 16MS
#include <cstdio>
#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
struct point{
int x, y;
};
const int dx[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const int dy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
bool map[105][105];
bool vis[105][105];
int N, M, res;
inline bool ok(int x, int y){
if(x >=0 && x < N && y >= 0 && y <M) return true;
else return false;
}
void bfs(int x, int y){
queue<point> q;
point h, n;
h.x = x, h.y = y;
q.push(h);
vis[h.x][h.y] = 1;
while(!q.empty()){
h = q.front();
q.pop();
for(int i=0; i<8; i++){
n.x = h.x + dx[i];
n.y = h.y + dy[i];
if(ok(n.x, n.y)){
if(map[n.x][n.y] == 1 && !vis[n.x][n.y]){
q.push(n);
vis[n.x][n.y] = 1;
}
}
}
}
}
int main(){
while(scanf("%d %d", &N, &M) != EOF){
char str[105];
for(int i=0; i<N; i++){
scanf("%s", str);
for(int j=0; j<strlen(str); j++)
map[i][j] = (str[j] == 'W' ? 1 : 0);
}
memset(vis, 0, sizeof(vis));
int res = 0;
for(int i=0; i<N; i++)
for(int j=0; j<M; j++){
if(map[i][j] == 1 && !vis[i][j]){
bfs(i, j);
res++;
}
}
printf("%d\n", res);
}
return 0;
}
POJ 2386
最新推荐文章于 2018-01-26 23:41:47 发布