zoukankan      html  css  js  c++  java
  • CodeForces 690D1 The Wall (easy) (判断连通块的数量)

    题意:给定一个图,问你有几个连通块。

    析:不用说了,最简单的DFS。

    代码如下:

    #include <bits/stdc++.h>
    
    using namespace std;
    const int maxn = 100 + 5;
    const int dr[] = {1, -1, 0, 0};
    const int dc[] = {0, 0, 1, -1};
    char a[maxn][maxn];
    int vis[maxn][maxn];
    
    void dfs(int r, int c){
        vis[r][c] = 1;
        for(int i = 0; i < 4; ++i){
            int x = r + dr[i];
            int y = c + dc[i];
            if(!vis[x][y] && a[x][y] == 'B')  dfs(x, y);
        }
    }
    
    int main(){
        int r, c;
        cin >> r >> c;
        getchar();
        memset(vis, 0, sizeof(vis));
        for(int i = 0; i < r; ++i)  cin >> a[i];
        int ans = 0;
        for(int i = 0; i < r; ++i)
            for(int j = 0; j < c; ++j)
                if(a[i][j] == 'B' && !vis[i][j]){
                    ++ans;
                    dfs(i, j);
                }
        cout << ans << endl;
        return 0;
    }
    
  • 相关阅读:
    curl continue
    actor
    nginx
    jmx additional port
    diff
    lsof
    zk reconnect
    Python:Python基础(一)
    Python:初识Python(二)
    Python:初识Python(一)
  • 原文地址:https://www.cnblogs.com/dwtfukgv/p/5660362.html
Copyright © 2011-2022 走看看