注意:障碍物是‘0’
#include iostream
#include queue
using namespace std;
int n,m,ans;
char map[101][101];
bool vis[101][101];
int xx[] = {-1,1,0,0};
int yy[] = {0,0,-1,1};
struct Node{
int x,y;
};
void bfs(int x,int y){
queue q;
q.push({x,y});
vis[x][y] = true;
while(!q.empty()){
Node u = q.front();
q.pop();
for(int i = 0; i < 4; i++){
if(nx < 1 || ny < 1 || nx > n || ny > m)
continue;
vis[nx][ny] = true;
q.push({nx, ny});
}
}
}
int main(){
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
}
}
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
bfs(i, j);
ans++;
}
}
}
}