• [Luogu] 家族


    https://www.luogu.org/problemnew/show/1767

    字符串的读入有点麻烦

    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #include <iostream>
    
    using namespace std;
    const int N = 110;
    
    #define yxy getchar()
    
    char A[N][N << 1];
    int n, Answer;
    int Vis[N][N << 1];
    struct Node{int x, y;};
    Node Queue[N * N];
    
    inline int read(){
        int x = 0; char c = yxy;
        while(c < '0' || c > '9') c = yxy;
        while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = yxy;
        return x;
    }
    
    void Bfs(int x, int y){
        int T = 1, H = 1;
        Node now; now.x = x, now.y = y;
        Queue[H] = now;
        while(H <= T){
            Node topp = Queue[H ++];
            int X = topp.x, Y = topp.y; Node nxt;
            Vis[X][Y] = 1;
            if(!Vis[X][Y + 1]) {nxt.x = X, nxt.y = Y + 1; Queue[++ T] = nxt;}
            if(!Vis[X][Y - 1]) {nxt.x = X, nxt.y = Y - 1; Queue[++ T] = nxt;}
            if(!Vis[X + 1][Y]) {nxt.x = X + 1, nxt.y = Y; Queue[++ T] = nxt;}
            if(!Vis[X - 1][Y]) {nxt.x = X - 1, nxt.y = Y; Queue[++ T] = nxt;}
        }
    }
    
    int main()
    {
        n = read();
        memset(Vis, -1, sizeof Vis);
        for(int i = 1; i <= n; i ++) gets(A[i] + 1);
        for(int i = 1; i <= n; i ++) {
            int len = strlen(A[i] + 1);
            for(int j = 1; j <= len; j ++){
                Vis[i][j] = 0;
                if(A[i][j] < 'a' || A[i][j] > 'z') Vis[i][j] = 1;    
            }
        }
        for(int i = 1; i <= n; i ++) {
            int len = strlen(A[i] + 1);
            for(int j = 1; j <= len; j ++){
                if(!Vis[i][j]) Answer ++, Bfs(i, j); 
            }
        }
        printf("%d", Answer);
        return 0;
    }
  • 相关阅读:
    CSP-S 2020 游记
    USACO Mowing the Lawn
    洛谷 P1725 琪露诺
    浅谈单调队列
    浅谈单调栈
    洛谷 P1440 求m区间内的最小值
    POJ 2823 Sliding Window
    洛谷 P1901 发射站
    POJ 2796 Feel Good
    POJ 2559 Largest Rectangle in a Histogram
  • 原文地址:https://www.cnblogs.com/shandongs1/p/8320250.html
Copyright © 2020-2023  润新知