PS/Java

[백준] 10026. 적록색약

siyamaki 2022. 7. 19. 14:37

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

 

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

 

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

RRRBB
GGBBB
BBBRR
BBRRR
RRRRR

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

 

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


입력

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

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

출력

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


예제 입력

5
RRRBB
GGBBB
BBBRR
BBRRR
RRRRR

예제 출력

4 3

적록색약/일반을 구분하기 위해 3차원 arr, visited배열을 만든다.

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.LinkedList;
import java.util.Queue;

public class Main {
    public static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    public static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
    public static int N;
    public static boolean[][][] visited;
    public static char[][][] arr;
    public static int[][] res;
    public static class Point {
        int r;
        int c;
        public Point(int r, int c) {
            this.r = r;
            this.c = c;
        }
    }
    public static void main(String[] args) throws Exception {
        N = Integer.parseInt(br.readLine());
        visited = new boolean[2][N][N];
        arr = new char[2][N][N];
        res = new int[2][1];
        for(int i = 0; i < N; i++) {
            String str = br.readLine();
            for(int j = 0; j < N; j++) {
                char c = str.charAt(j);
                arr[0][i][j] = c;
                if(c == 'G') {
                    arr[1][i][j] = 'R';
                } else {
                    arr[1][i][j] = c;
                }
            }
        }
        findArea(0);
        findArea(1);
        // div = 0 > 정상 div = 1 > 비정상
        bw.write(res[0][0] + " " + res[1][0]);
        br.close();
        bw.flush();
        bw.close();
    }

    public static void findArea(int div) {
        for(int i = 0; i < N; i++) {
            for(int j = 0; j < N; j++) {
                if(!visited[div][i][j]) {
                    bfs(i, j, div);
                    res[div][0]++;
                }
            }
        }
    }
    public static int[] dr = {-1, 1, 0, 0};
    public static int[] dc = {0, 0, -1, 1};
    public static void bfs(int cr, int cc, int div) {
        Queue<Point> queue = new LinkedList<>();
        queue.add(new Point(cr, cc));
        visited[div][cr][cc] = true;
        while(!queue.isEmpty()) {
            Point current = queue.poll();

            for(int d = 0; d < 4; d++) {
                int nr = current.r + dr[d];
                int nc = current.c + dc[d];
                if(nr >= 0 && nr < N && nc >= 0 && nc < N && !visited[div][nr][nc] && arr[div][current.r][current.c] == arr[div][nr][nc]) {
                    queue.add(new Point(nr, nc));
                    visited[div][nr][nc] = true;
                }
            }
        }
    }
}

적록색약(구분값이 1)일 때 arr의 값이 G이면 R로 바꾼다.

나머지 bfs 탐색은 동일하게 진행한다.