1 条题解

  • 0
    @ 2024-08-22 15:40:38
    #include<bits/stdc++.h>
    #define N 110
    using namespace std;
    bool b[N][N], vis[N][N];
    int p[5][3] = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}}, m, n;
    struct node{
        int x, y;
    }q[N];
    void bfs(int x, int y){
        int head = 1, tail = 1;
        q[tail].x = x; q[tail].y = y;
        tail++;
        while(head < tail){
            vis[q[head].x][q[head].y] = 1;
            for(int i = 0; i < 4; i++){
                int nx = q[head].x + p[i][0], ny = q[head].y + p[i][1];
                if(nx >= 1 && nx <= m  &&  ny >= 1 && ny <= n  &&  b[nx][ny]  &&  !vis[nx][ny]){
                    q[tail].x = nx; q[tail].y = ny;  //入队
                    tail++;
                    vis[nx][ny] = 1;
                }
            }
            head++;  //队头出队 
        }
    }
    int main(){
        ios::sync_with_stdio(false);
        int cnt = 0;
        cin >> m >> n;
        memset(vis, 0, sizeof(vis));
        for(int i = 1; i <= m; i++){
            for(int j = 1; j <= n; j++)cin >> b[i][j];
        }
        for(int i = 1; i <= m; i++){
            for(int j = 1; j <= n; j++){
                if(b[i][j] && !vis[i][j]){
                    cnt++;
                    bfs(i, j);
                }
            }
        }
        cout << cnt;
        return 0;
    }
    
  • 1

信息

难度
6
分类
(无)
标签
递交数
120
已通过
30
通过率
25%
被复制
7
上传者