[백준 10026] 적록색약
Algorithm/백준(BOJ)

[백준 10026] 적록색약

반응형

[백준 10026] 적록색약


문제 출처 : https://www.acmicpc.net/problem/10026







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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
 
public class Problem10026 {
    
    static char map[][];
    static boolean visited[][];
    static int n;
    static int dx[] = {00-11};
    static int dy[] = {-1100};
    
    static int count = 0;
    static int count2 = 0;
    
    public static void dfs(int x, int y){
        visited[x][y] = true;
        
        char c = map[x][y];
        
        for (int i = 0; i < 4; i++) {
            int nx = x + dx[i];
            int ny = y + dy[i];
            
            if(nx>=0 && nx < n && ny>=0 && ny<&& !visited[nx][ny]){
                if(map[nx][ny] == c){
                    dfs(nx,ny);
                }
            }
        }
    }
    
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        
        n = Integer.parseInt(br.readLine());
        
        map = new char[n][n];
        
        for (int i = 0; i < n; i++) {
            String[] str = br.readLine().split("");
            for (int j = 0; j < n; j++) {
                map[i][j] = str[j].charAt(0);
            }
        }
        
        visited = new boolean[n][n];
        
        //정상
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if(visited[i][j] == false){
                    count++;
                    dfs(i,j);
                }
            }
        }
        
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if(map[i][j] == 'G')
                    map[i][j] = 'R';
            }
        }
        
        visited = new boolean[n][n];
        
        //적록 색약
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if(visited[i][j] == false){
                    count2++;
                    dfs(i,j);
                }
            }
        }
        
        System.out.println(count + " " + count2);
    }
}
 
cs

반응형

'Algorithm > 백준(BOJ)' 카테고리의 다른 글

[백준 2178] 미로 탐색  (0) 2019.02.02
[백준 1260] DFS와 BFS - 인접 행렬 이용  (0) 2019.02.02
[백준 2468] 안전 영역  (0) 2019.01.29
[백준 2667] 단지번호 붙이기  (0) 2019.01.29
[백준 1874] 스택 수열  (1) 2019.01.24