[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;
}

 

posted @ 2018-01-20 11:47  xayata  阅读(107)  评论(0编辑  收藏  举报