적록색약

Huisu·2023년 8월 10일
0

Coding Test Practice

목록 보기
25/98
post-thumbnail

문제

10026번: 적록색약

문제 설명

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

크기가 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
RRRRR4 3

아이디어

그냥 흔한 BFS

제출 코드

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;

// https://www.acmicpc.net/problem/10026
public class five10026 {
    String[][] grid;
    int[][] cowArt;
    int[][] notCowArt;
    boolean[][] visited;
    int n;
    int cowArtNum = 0;
    int notCowArtNum = 0;
    int[] dr = new int[] {0, 0, 1, -1};
    int[] dc = new int[] {1, -1, 0, 0};
    public void solution() throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        n = Integer.parseInt(reader.readLine());
        grid = new String[n][n];
        visited = new boolean[n][n];
        cowArt = new int[n][n];
        notCowArt = new int[n][n];

        for (int i = 0; i < n; i++) {
            grid[i] = reader.readLine().split("");
        }

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (grid[i][j].equals("R")) {
                    notCowArt[i][j] = 1;
                    cowArt[i][j] = 1;
                }
                else if (grid[i][j].equals("G")) {
                    notCowArt[i][j] = 2;
                    cowArt[i][j] = 1;
                }
                else {
                    notCowArt[i][j] = 3;
                    cowArt[i][j] = 2;
                }
            }
        }

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (!visited[i][j])
                    cowArtBfs(i, j);
            }
        }

        for (boolean[] row: visited) {
            Arrays.fill(row, false);
        }

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (!visited[i][j])
                    notCowArtBfs(i, j);
            }
        }
        System.out.printf("%d %d", notCowArtNum, cowArtNum);
    }

    private void cowArtBfs(int row, int col) {
        Queue<int[]> toVisit = new LinkedList<>();
        toVisit.offer(new int[] {row, col});
        while(!toVisit.isEmpty()) {
            int[] now = toVisit.poll();
            int nowRow = now[0];
            int nowCol = now[1];
            visited[nowRow][nowCol] = true;
            int currentColor = cowArt[nowRow][nowCol];
            for (int i = 0; i < 4; i++) {
                int nextRow = nowRow + dr[i];
                int nextCol = nowCol + dc[i];
                if (checkBoundary(nextRow, nextCol) &&
                    cowArt[nextRow][nextCol] == currentColor &&
                    !visited[nextRow][nextCol]) {
                    visited[nextRow][nextCol] = true;
                    toVisit.offer(new int[] {nextRow, nextCol});
                }
            }
        }
        cowArtNum++;
    }

    private void notCowArtBfs(int row, int col) {
        Queue<int[]> toVisit = new LinkedList<>();
        toVisit.offer(new int[] {row, col});
        while(!toVisit.isEmpty()) {
            int[] now = toVisit.poll();
            int nowRow = now[0];
            int nowCol = now[1];
            visited[nowRow][nowCol] = true;
            int currentColor = notCowArt[nowRow][nowCol];
            for (int i = 0; i < 4; i++) {
                int nextRow = nowRow + dr[i];
                int nextCol = nowCol + dc[i];
                if (checkBoundary(nextRow, nextCol) &&
                        notCowArt[nextRow][nextCol] == currentColor &&
                        !visited[nextRow][nextCol]) {
                    visited[nextRow][nextCol] = true;
                    toVisit.offer(new int[] {nextRow, nextCol});
                }
            }
        }
        notCowArtNum++;
    }

    private boolean checkBoundary(int nextRow, int nextCol) {
        return -1 < nextRow && nextRow < n
                && -1 < nextCol && nextCol < n;
    }

    public static void main(String[] args) throws IOException {
        new five10026().solution();
    }
}

0개의 댓글