zoukankan      html  css  js  c++  java
  • Aizu 0118

    Aizu 0118

    BFS 连通块

    多个不同的连通块问题

    #include <iostream>
    #include <cstring>
    #include <algorithm>
    #include <queue>
    using namespace std;
    #define endl '
    '
    const int N = 200;
    char g[N][N];
    int n,m,ans,sx,sy;
    int dx[4] = {0,0,1,-1};
    int dy[4] = {1,-1,0,0};
    bool st[N][N];
    struct node {
        int x,y;
    };
    void bfs(int x,int y) {
        queue<node> q;
        q.push({x,y});
        st[x][y] = 1;
        while(q.size()) {
            node t = q.front();
            q.pop();
            for(int i = 0;i < 4; ++i) {
                int nx = t.x + dx[i],ny = t.y + dy[i];
                if(nx >= 0 && nx < n && ny >= 0 && ny < m && g[nx][ny] == g[x][y] && !st[nx][ny]) {
                    q.push({nx,ny});
                    st[nx][ny] = 1;
                }
            }
        }
    }
    int main() {
        ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
        while(cin >> n >> m && n && m) {
            memset(st,0,sizeof st);
            ans = 0;
            for(int i = 0;i < n; ++i)
                for(int j = 0;j < m; ++j) 
                    cin >> g[i][j];
            for(int i = 0;i < n; ++i) {
                for(int j = 0;j < m; ++j) {
                    if(!st[i][j]) {
                        bfs(i,j);
                        ans ++;
                    }
                }
            }
            cout << ans << endl;
        }
        return 0;
    }
    
  • 相关阅读:
    http
    python的列表生成式
    flask的登陆验证
    脚本更新流程
    k8s中job和pod的区别
    k8s中一些常见概念
    supervisord部署和使用
    flask中config
    python类的继承super()的使用
    python中类的继承
  • 原文地址:https://www.cnblogs.com/lukelmouse/p/12430566.html
Copyright © 2011-2022 走看看