考查点:广搜或深搜
思路:主要注意输出的处理,需要用getchar两次接收换行,剩下的基本上是标准的深搜或广搜
#define LOCAL
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#define FOR(i, x, y) for(int i = x; i < y; i++)
#define rFOR(i, x, y) for(int i = x; i > = y; i--)
#define MAXN 10005
#define oo 0x3f3f3f3f
using namespace std;
int m,n,l,t;
char pi[120][130];
int X[8]={0,0,1,-1,1,-1,1,-1};
int Y[8]={1,-1,0,0,1,-1,-1,1};
int inq[129][130];
struct node{
int x,y;
}Node;
bool judge(int x,int y){
if(x<0 || y<0) return false;
if(x>=m||y>=n) return false;
if(inq[x][y]||pi[x][y]=='.') return false;
return true;
}
void BFS(int x,int y){
queue<node> q;
Node.x=x,Node.y=y;
q.push(Node);
inq[x][y]=1;
while(!q.empty()){
node top=q.front();
q.pop();
FOR(i,0,8){
int nx=top.x+X[i];
int ny=top.y+Y[i];
if(judge(nx,ny)){
Node.x=nx,Node.y=ny;
q.push(Node);
inq[nx][ny]=1;
}
}
}
}
int main()
{
#ifdef LOCAL
freopen("data.in","r",stdin);
freopen("data.out","w",stdout);
#endif // LOCAL
scanf("%d%d",&m,&n);
getchar();
FOR(j,0,m){
FOR(k,0,n){
scanf("%c",&pi[j][k]);
}
getchar();
}
int ans=0;
FOR(j,0,m)
FOR(k,0,n){
if(pi[j][k]=='W'&&inq[j][k]==0)
{
BFS(j,k);ans++;
}
}
printf("%d",ans);
return 0;
}