본문 바로가기
알고리즘/DFS

10026 적록색약

by tryotto 2019. 7. 30.
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <stdio.h>
#include <string.h>
 
using namespace std;
 
int rst1 = 0, rst2 = 0;
int arr1[105][105= { 0 };
int arr2[105][105= { 0 };
int check[105][105= { 0 };
 
void DFS1(int y, int x, int color);
void DFS2(int y, int x, int color);
 
int main() {
    int n;
    scanf("%d"&n);
    getchar();
 
    for (int i = 1; i <= n; i++) {
        char str[105];
        scanf("%s"&str);
 
        for (int j = 0; j < n; j++) {    
            switch (str[j]) {
                case 'R':
                    arr1[i][j+1= 1;
                    arr2[i][j+1= 1;
                    break;
                case 'G':
                    arr1[i][j+1= 2;
                    arr2[i][j+1= 1;
                    break;
                case 'B':
                    arr1[i][j+1= 3;
                    arr2[i][j+1= 3;
                    break;
            }
        }
    }
 
    int rst = 0;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            if (check[i][j] == 0) {
                rst++;
                check[i][j] = 1;
                DFS1(i, j,arr1[i][j]);
            }
        }
    }
    printf("%d ", rst);
    
    rst = 0;
    for (int i = 1; i <= n; i++) {
        memset(check[i], 0sizeof(int* 105);
    }
 
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            if (check[i][j] == 0) {
                rst++;
                check[i][j] = 1;
                DFS2(i, j, arr2[i][j]);
            }
        }
    }
    printf("%d", rst);
}
 
void DFS1(int y, int x, int color) {
    if (arr1[y - 1][x] == color && check[y - 1][x] == 0) {
        check[y - 1][x] = 1;
        DFS1(y - 1, x, color);
    }
    if (arr1[y + 1][x] == color && check[y + 1][x] == 0) {
        check[y + 1][x] = 1;
        DFS1(y + 1, x, color);
    }
    if (arr1[y][x-1== color && check[y][x-1== 0) {
        check[y][x-1= 1;
        DFS1(y, x-1, color);
    }
    if (arr1[y][x + 1== color && check[y][x + 1== 0) {
        check[y][x + 1= 1;
        DFS1(y, x + 1, color);
    }
}
 
void DFS2(int y, int x, int color) {
    if (arr2[y - 1][x] == color && check[y - 1][x] == 0) {
        check[y - 1][x] = 1;
        DFS2(y - 1, x, color);
    }
    if (arr2[y + 1][x] == color && check[y + 1][x] == 0) {
        check[y + 1][x] = 1;
        DFS2(y + 1, x, color);
    }
    if (arr2[y][x - 1== color && check[y][x-1== 0) {
        check[y][x - 1= 1;
        DFS2(y, x - 1, color);
    }
    if (arr2[y][x + 1== color && check[y][x+1== 0) {
        check[y][x + 1= 1;
        DFS2(y, x + 1, color);
    }
}
cs


'알고리즘 > DFS' 카테고리의 다른 글

6603 로또  (0) 2019.07.31
2583 영역 구하기  (0) 2019.07.30
11724 연결요소의 갯수  (0) 2019.07.30
11403 경로찾기  (0) 2019.07.30
1012 유기농 배추  (0) 2019.07.29