[백준 알고리즘] 10026번 : 적록색약

문제

적록색약은 빨간색과 초록색의 차이를 거의 느끼지 못한다. 따라서, 적록색약인 사람이 보는 그림은 아닌 사람이 보는 그림과는 좀 다를 수 있다.

크기가 N×N인 그리드의 각 칸에 R(빨강), G(초록), B(파랑) 중 하나를 색칠한 그림이 있다. 그림은 몇 개의 구역으로 나뉘어져 있는데, 구역은 같은 색으로 이루어져 있다. 또, 같은 색상이 상하좌우로 인접해 있는 경우에 두 글자는 같은 구역에 속한다. (색상의 차이를 거의 느끼지 못하는 경우도 같은 색상이라 한다)

예를 들어, 그림이 아래와 같은 경우에

RRRBB
GGBBB
BBBRR
BBRRR
RRRRR
적록색약이 아닌 사람이 봤을 때 구역의 수는 총 4개이다. (빨강 2, 파랑 1, 초록 1) 하지만, 적록색약인 사람은 구역을 3개 볼 수 있다. (빨강-초록 2, 파랑 1)

그림이 입력으로 주어졌을 때, 적록색약인 사람이 봤을 때와 아닌 사람이 봤을 때 구역의 수를 구하는 프로그램을 작성하시오.

입력

첫째 줄에 N이 주어진다. (1 ≤ N ≤ 100)

둘째 줄부터 N개 줄에는 그림이 주어진다.

출력

적록색약이 아닌 사람이 봤을 때의 구역의 개수와 적록색약인 사람이 봤을 때의 구역의 수를 공백으로 구분해 출력한다.

코드

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class BOJ_10026 {
    static char[][] grid;
    static boolean[][] normalVisit, colorBlindVisit;
    static int[] moveX = {1, -1, 0, 0};
    static int[] moveY = {0, 0, 1, -1};
    static int n;

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        n = Integer.parseInt(br.readLine());

        normalVisit = new boolean[n+1][n+1];
        colorBlindVisit = new boolean[n+1][n+1];
        grid = new char[n+1][n+1];

        for (int i = 0; i < n; i++) {
            char[] str = br.readLine().toCharArray();
            for (int j = 0; j < n; j++) {
                grid[i][j] = str[j];
            }
        }

        int normalCount = 0;
        int colorBlindCount = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (!normalVisit[i][j]) {
                    normalDFS(i, j, grid[i][j], normalCount);
                    ++normalCount;
                }

                if (!colorBlindVisit[i][j]) {
                    colorBlindDFS(i, j, grid[i][j], colorBlindCount);
                    ++colorBlindCount;
                }
            }
        }
        System.out.println(normalCount + " " + colorBlindCount);
    }

    static void normalDFS(int x, int y, char color, int normalCount) {
        normalVisit[x][y] = true;

        for (int i = 0; i < 4; i++) {
            int nextX = x + moveX[i];
            int nextY = y + moveY[i];

            if (nextX < 0 || nextY < 0 || nextX >= n || nextY >= n) continue;
            if (normalVisit[nextX][nextY]) continue;

            char nextColor = grid[nextX][nextY];
            if (nextColor == color) normalDFS(nextX, nextY, nextColor, normalCount);
        }
    }

    static void colorBlindDFS(int x, int y, char color, int colorBlindCount) {
        colorBlindVisit[x][y] = true;

        for (int i = 0; i < 4; i++) {
            int nextX = x + moveX[i];
            int nextY = y + moveY[i];

            if (nextX < 0 || nextY < 0 || nextX >= n || nextY >= n) continue;
            if (colorBlindVisit[nextX][nextY]) continue;

            char nextColor = grid[nextX][nextY];
            if (nextColor == color) {
                colorBlindDFS(nextX, nextY, nextColor, colorBlindCount);
            } else {
                if ((color == 'R' && nextColor == 'G') || (color == 'G' && nextColor == 'R')) {
                    colorBlindDFS(nextX, nextY, nextColor, colorBlindCount);
                }
            }
        }
    }
}

풀이 및 느낀점

dfs를 사용하여 문제에 접근했다.

색약이 아닌 사람의 dfs와 색약인 사람의 dfs를 구분하였다.

색약이 아닌 사람의 dfs는 먼저 현재 격자의 방문여부를 true로 저장하고 다음 위치의 격자를 탐색하였다. 다음 위치의 격자가 범위를 벗어나지 않고 방문하지 않았다면, 다음 위치의 격자 색깔과 현재 위치의 격자 색깔을 비교하였다. 색깔이 같다면, 다시 색약이 아닌 사람의 dfs를 재귀 호출하여 다음 위치의 격자를 탐색했다.

색약인 사람의 dfs 또한 색약이 아닌 사람의 dfs와 비슷하다. 하지만 차이점이 있다면 색약인 사람은 빨간색과 초록색이 인접해 있을 때 같은 색으로 인식한다. 따라서 조건문을 추가하여 두가지의 색깔을 같다고 여기고 dfs를 재귀호출하여 다음 위치의 격자를 탐색했다.

참고자료

좋은 웹페이지 즐겨찾기