#4130. 广搜—联通块问题—模版

广搜—联通块问题—模版

注意:障碍物是‘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++){

            int nx = u.x + {{ input(1) }}; 填空1:新的x坐标

            int ny = u.y + {{ input(2) }}; 填空2:新的y坐标

            if(nx < 1 || ny < 1 || nx > n || ny > m)

                continue;

            if({{ input(3) }}) continue; // 填空3:判断是否访问过

            if(map[nx][ny] == {{ input(4) }}) continue; // 填空4:障碍物判断

            vis[nx][ny] = true;

            q.push({nx, ny});

        }

    }

}

int main(){

    cin >> {{ input(5) }} >> {{ input(6) }}; // 填空5,6:读入行和列

    for(int i = 1; i <= n; i++){

        for(int j = 1; j <= m; j++){

            cin >> {{ input(7) }}; 填空7:输入地图

        }

    }

    for(int i = 1; i <= n; i++){

        for(int j = 1; j <= m; j++){

            if(!vis[i][j] && {{ input(8) }}){ 填空8:起点为什么状态可以搜索 注意障碍物不要加引号

                bfs(i, j);

                ans++;

            }

        }

    }

    cout << {{ input(9) }}; 填空9:输出数量

    return {{ input(10) }}; 填空10:返回值

}