简简单单轻轻松松
2024-09-07 17:13:06
发布于:广东
1阅读
0回复
0点赞
#include<bits/stdc++.h>
using namespace std;
int dx[8] = {1,-1,0,0,1,1,-1,-1},dy[8] = {0,0,1,-1,1,-1,1,-1},vis[120][120];
char a[120][120];
struct node{
int x,y;
};
queue<node> q;
void bfs(){
while(!q.empty()){
int ix = q.front().x,iy = q.front().y;
q.pop();
vis[ix][iy] = 1;
for(int i = 0;i < 8;i ++){
int nx = ix + dx[i],ny = iy + dy[i];
if(nx >= 0 and ny >= 0 and a[nx][ny] == 'W' and vis[nx][ny] == 0){
q.push({nx,ny});
}
}
}
}
int main(){
int n,m,ans = 0;
cin >> n >> m;
for(int i = 0;i < n;i ++) for(int j = 0;j < m;j ++) cin >> a[i][j];
for(int i = 0;i < n;i ++){
for(int j = 0;j < m;j ++){
if(a[i][j] == 'W' and vis[i][j] == 0){
q.push({i,j});
ans ++;
bfs();
}
}
}
cout << ans;
return 0;
}
这里空空如也
有帮助,赞一个