连通块问题
2024-09-29 20:26:36
发布于:广东
3阅读
0回复
0点赞
#include<bits/stdc++.h>
using namespace std;
int vis[105][105],dx[8] = {1,-1,0,0,1,1,-1,-1},dy[8] = {0,0,1,-1,1,-1,1,-1},n,m;
char a[105][105];
void DFS(int x,int y){
vis[x][y] = 1;
for(int i = 0;i < 8;i ++){
int nx = x + dx[i];
int ny = y + dy[i];
if(nx >= 0 and ny >= 0 and nx < n and ny < m and vis[nx][ny] == 0 and a[nx][ny] == 'W'){
vis[nx][ny] = 1;
DFS(nx,ny);
}
}
}
int main(){
cin >> n >> m;
for(int i = 0;i < n;i ++){
for(int j = 0;j < m;j ++){
cin >> a[i][j];
}
}
int ans = 0;
for(int i = 0;i < n;i ++){
for(int j = 0;j < m;j ++){
if(vis[i][j] == 0 and a[i][j] == 'W'){
ans ++;
DFS(i,j);
}
}
}
cout << ans;
return 0;
}
这里空空如也
有帮助,赞一个