Home [BOJ 10026] 적록색약
Post
Cancel

[BOJ 10026] 적록색약

적록색약 (10026번)

https://www.acmicpc.net/problem/10026

뻘짓

  • 단순히 생각하면 되는데 어렵게 생각했다.
  • 적록색약인 경우 R인지 G인지 확인하고 상하좌우의 값이 R 또는 G인 경우에만 더해주는 방식으로 구현해보려했는데 잘 되지 않았다.

풀이방법

  1. BFS를 이용해 적록색약이 아닐 때의 개수를 센다.
  2. RG로 바꾸게 되면 적록색약인 사람이 보는 것과 동일하다.
  3. RG로 바꾼 후 다시 BFS를 이용해 적록색약일 때의 개수를 센다.
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.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;

public class Main {
    static char[][] map;
    static boolean[][] visited;
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int N = Integer.parseInt(br.readLine());
        map = new char[N][N];
        visited = new boolean[N][N];
        for (int i=0; i<N; i++) {
            String str = br.readLine();
            for (int j=0; j<N; j++) {
                map[i][j] = str.charAt(j);
            }
        }

        int rgbCount = 0;
        int rrbCount = 0;
        for (int i=0; i<N; i++) {
            for (int j=0; j<N; j++) {
                if (!visited[i][j]) {
                    char c = map[i][j];
                    bfs(c, j, i, N);
                    rgbCount++;
                }
            }
        }

        changeMap(N);
        visited = new boolean[N][N];
        for (int i=0; i<N; i++) {
            for (int j=0; j<N; j++) {
                if (!visited[i][j]) {
                    char c = map[i][j];
                    bfs(c, j, i, N);
                    rrbCount++;
                }
            }
        }

        StringBuilder sb = new StringBuilder();
        sb.append(rgbCount).append(' ').append(rrbCount);
        System.out.println(sb);
    }

    public static void changeMap(int N) {
        for (int i=0; i<N; i++) {
            for (int j=0; j<N; j++) {
                if (map[i][j] == 'R') map[i][j] = 'G';
            }
        }
    }

    public static void bfs(char c, int x, int y, int n) {
        Queue<int[]> queue = new LinkedList<>();
        queue.add(new int[] { x, y });
        visited[y][x] = true;
        int[] cur;
        int[] xPos = {1, 0, -1, 0};
        int[] yPos = {0, 1, 0, -1};
        int dx = 0; int dy = 0;
        while (!queue.isEmpty()) {
            cur = queue.poll();

            for (int i=0; i<4; i++) {
                dx = cur[0] + xPos[i];
                dy = cur[1] + yPos[i];

                if (dx < 0 || dx >= n || dy < 0 || dy >= n) continue;
                if (visited[dy][dx] || map[dy][dx] != c) continue;
                visited[dy][dx] = true;
                queue.add(new int[] { dx, dy });
            }
        }
    }
}

This post is licensed under CC BY 4.0 by the author.

[BOJ 1012] 유기농 배추

[BOJ 7569] 토마토

Comments powered by Disqus.