Post

백준 #10026 적록색약

백준 #10026 적록색약

RGB로 이루어진 배열의 덩어리 개수와 RG를 하나로 보았을 때의 덩어리 개수를 구하는 문제.

R 덩어리, G 덩어리, B 덩어리, RG 덩어리 개수를 각가 BFS로 구하고, R G B 덩어리 개수의 합과 RG와 B 덩어리 개수의 합을 출력했다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

int N;
char board[100][100];
bool visit[100][100];
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};

void consumeBuffer() {
    while(getchar() != '\n') {}
}

bool isInBoard(int x, int y) {
    return x >= 0 && x < N && y >= 0 && y < N;
}

void dfs(char c, pair<int, int> start, bool blind) {
    visit[start.first][start.second] = true;
    for(int i = 0; i < 4; i++) {
        int nX = start.first + dx[i];
        int nY = start.second + dy[i];
        if(!blind) {
            if(isInBoard(nX, nY) && !visit[nX][nY] && board[nX][nY] == c) {
                dfs(c, {nX, nY}, blind);
            }
        }
        else {
            if(isInBoard(nX, nY) && !visit[nX][nY] && (board[nX][nY] == 'R' || board[nX][nY] == 'G')) {
                dfs(c, {nX, nY}, blind);
            }
        }
    }
}

int countArea(char c, bool blind) {
    memset(visit, 0, sizeof(visit));
    int count = 0;
    for(int i = 0; i < N; i++) {
        for(int j = 0; j < N; j++) {
            if(!blind) {
                if(board[i][j] == c && !visit[i][j]) {
                    count++;
                    dfs(c, {i, j}, blind);
                }
            }
            else {
                if((board[i][j] == 'R' || board[i][j] == 'G') && !visit[i][j]) {
                    count++;
                    dfs(c, {i, j}, blind);
                }
            }
        }
    }
    return count;
}

int main() {
    cin >> N;
    int rCount = 0;
    int gCount = 0;
    int bCount = 0;
    int rgCount = 0;
    consumeBuffer();
    for(int i = 0; i < N; i++) {
        for(int j = 0; j < N; j++) {
            board[i][j] = getchar();
        }
        consumeBuffer();
    }
    rCount = countArea('R', false);
    gCount = countArea('G', false);
    bCount = countArea('B', false);
    rgCount = countArea('X', true);
    cout << rCount + gCount + bCount << ' ' << rgCount + bCount;
    return 0;
    
}
This post is licensed under CC BY 4.0 by the author.