RGB로 이루어진 n*n 짜리를 입력받아 적록색약인 경우, 아닌경우로 나눠 몇개의 구역으로 나뉘어 있는지 출력하는 문제다.


bfs, dfs 원하는 것 취사선택.


a[k][i][j] k : 0 = 적록색약인 경우, 1 = 아닌경우


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
#include <string>
#include <iostream>
using namespace std;
 
int n, a[2][101][101], dx[] = { 0,0,1,-1 }, dy[] = { 1,-1,0,0 }, cnt[2];
 
void dfs(int x, int y, int p, int c) {
    a[p][x][y] = -1;
    for (int i = 0; i < 4; i++) {
        int nx = x + dx[i], ny = y + dy[i];
        if (nx < 0 || ny < 0 || nx >= n || ny >= n || a[p][nx][ny] != c) continue;
        dfs(nx, ny, p, c);
    }
}
 
int main() {
    ios::sync_with_stdio(false);
    cin >> n;
    for (int i = 0; i < n; i++) {
        string s;
        cin >> s;
        for (int j = 0; j < n; j++) {
            if (s[j] == 'B') a[0][i][j] = a[1][i][j] = 1;
            else if (s[j] == 'R') a[0][i][j] = a[1][i][j] = 2;
            else if (s[j] == 'G') a[0][i][j] = 2, a[1][i][j] = 3;
        }
    }
    for (int i = 0; i < n; i++for (int j = 0; j < n; j++for (int k = 0; k < 2; k++) {
        if (a[k][i][j] != -1) {
            dfs(i, j, k, a[k][i][j]);
            cnt[k]++;
        }
    }
    cout << cnt[1<< " " << cnt[0<< '\n';
    return 0;
}
cs


'BOJ' 카테고리의 다른 글

10319 좀비 아포칼립스  (0) 2017.09.05
1574 룩 어택  (0) 2017.09.04
6603 로또  (0) 2017.09.01
2644 촌수계산  (0) 2017.09.01
2778 측량사 지윤  (0) 2017.09.01

+ Recent posts