백준 10026 풀이

남기용·2021년 4월 20일
0

백준 풀이

목록 보기
47/109

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


적록색약

풀이

대충 생각대로 작성했는데 정답이었다. 효율은...

크게 경우의 수가 2개로 나뉜다.

  • 적록색약인 사람
  • 아닌 사람

이 두가지 경우에 대해 구현을 해야는데 type으로 나눠서 구현을 했다. 그래서 입력 배열도 2개, 함수도 type 변수를 통해 비슷하지만 다르게 구현을 했다.
효율이 별로 같지만 일단 그렇게 작성을 했다.

R,G,B 값 각각을 1,2,3으로 배열에 저장해서 구현을 조금 더 편하게 한 것도 있다.

이 후는 딱히 별로 할 말이 없는 것이 R인 곳 찾아서 bfs하고... G인 곳 찾아서 bfs하고... B인 곳 찾아서 bfs하고...

이런 순서였다. 적록색약인 경우 G인 공간을 R로 치환해서 저장했기때문에 더욱 간단했다.


import java.io.*;
import java.util.LinkedList;
import java.util.Queue;

public class Main {
    static int[] dx = {0, 0, -1, 1};
    static int[] dy = {1, -1, 0, 0};
    static boolean visit[][];
    static int n;
    static int graph[][];
    static int graph2[][];
    static Queue<Pair> queue;
    static int cnt = 0;
    static int cnt1 = 0;

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        n = Integer.parseInt(br.readLine());
        graph = new int[n][n];
        graph2 = new int[n][n];
        visit = new boolean[n][n];
        queue = new LinkedList<>();

        for (int i = 0; i < n; i++) {
            String[] line = br.readLine().split("");
            for (int j = 0; j < n; j++) {
                graph[i][j] = colorToInt(line[j], 1);
                graph2[i][j] = colorToInt(line[j], 2);
            }
        }
        for (int i = 1; i <= 3; i++) {
            find(i, graph, 1);
        }
        visit = new boolean[n][n];

        for (int i = 1; i <= 3; i++) {
            find(i, graph2, 0);
        }

        bw.write(cnt + " " + cnt1);
        br.close();
        bw.close();
    }

    private static void find(int num, int[][] graph, int type) {
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (graph[i][j] == num && !visit[i][j]) {
                    bfs(i, j, num, graph, type);
                }
            }
        }
    }

    private static void bfs(int X, int Y, int num, int[][] graph, int type) {
        Pair pair = new Pair(X, Y);
        queue.offer(pair);
        while (!queue.isEmpty()) {
            Pair p = queue.poll();
            int x = p.x;
            int y = p.y;
            visit[x][y] = true;
            for (int i = 0; i < 4; i++) {
                if ((x + dx[i] >= 0 && x + dx[i] <= n - 1) && (y + dy[i] >= 0 && y + dy[i] <= n - 1)) {
                    if (graph[x + dx[i]][y + dy[i]] == num && !visit[x + dx[i]][y + dy[i]]) {
                        visit[x + dx[i]][y + dy[i]] = true;
                        queue.offer(new Pair(x + dx[i], y + dy[i]));
                    }
                }
            }
        }
        if(type == 1)
            cnt++;
        else
            cnt1++;
    }

    private static int colorToInt(String color, int type) {
        // color 1==R, 2==G, 3==B
        // type 1==normal 2==color weakness
        if (type == 1) {
            switch (color) {
                case "R":
                    return 1;
                case "G":
                    return 2;
                case "B":
                    return 3;
            }
        } else {
            switch (color) {
                case "R":
                case "G":
                    return 1;
                case "B":
                    return 3;
            }
        }
        return 0;
    }
}

class Pair {
    public int x;
    public int y;

    public Pair(int x, int y) {
        this.x = x;
        this.y = y;
    }
}
profile
개인용 공부한 것을 정리하는 블로그입니다.

0개의 댓글