zoukankan      html  css  js  c++  java
  • POJ 1979

    POJ 1979

    BFS 连通块

    输入的(n)(m) 是反的要注意

    #include <iostream>
    #include <cstdio>
    #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 < 8; ++i) {
                int nx = t.x + dx[i],ny = t.y + dy[i];
                if(nx >= 0 && nx < n && ny >= 0 && ny < m && g[nx][ny] == '.' && !st[nx][ny]) {
                    q.push({nx,ny});
                    st[nx][ny] = 1;
                    ans ++;
                }
            }
        }
    }
    int main() {
        ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
        while(cin >> m >> n && n && m) {
            memset(st,0,sizeof st);
            ans = 1;
            for(int i = 0;i < n; ++i)
                for(int j = 0;j < m; ++j) {
                    cin >> g[i][j];
                    if(g[i][j] == '@') sx = i,sy = j;
                }
            bfs(sx,sy);
            cout << ans << endl;
        }
        return 0;
    }
    
  • 相关阅读:
    NOIp2018集训test-9-23
    NOIp2018集训test-9-22(am/pm) (联考三day1/day2)
    NOIp2018集训test-9-21(am/pm)
    NOIp2018集训test-9-19(am&pm)
    day41.txt
    day40表关系
    day39
    day38数据库
    day37
    day36
  • 原文地址:https://www.cnblogs.com/lukelmouse/p/12427998.html
Copyright © 2011-2022 走看看