[백준] 10026번 : 적록색약 - JAVA [자바]

가오리·2024년 1월 24일
0
post-thumbnail

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


bfs 알고리즘 문제이다.

  1. map 을 두개로 나눠서 입력을 받을때, 적록색약이 아닌 사람은 그대로 입력받고 적록색약인 사람은 GR로 입력 받도록 하였다.
  2. map1을 돌면서 아직 방문하지 않은 위치이면 방문하고 그 위치와 인접하며 같은 색의 위치는 같은 그룹으로 묶으며 방문 처리를 하며 그룹의 갯수를 구한다.
  3. 2번과 같은 방법으로 적록색약인 사람인 경우도 적록색약 전용 맵 map2를 돌면서 그룹의 갯수를 구한다.

public class bj10026 {
    static char[][] map1, map2;
    static int[] xMove = {-1, 0, 1, 0};
    static int[] yMove = {0, -1, 0, 1};
    static int N;
    static boolean[][] visited;

    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

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

        map1 = new char[N][N];
        map2 = new char[N][N];

        for (int i = 0; i < N; i++) {
            String line = br.readLine();
            char[] charArray = line.toCharArray();
            for (int j = 0; j < N; j++) {
                map1[i][j] = charArray[j];
                if (charArray[j] == 'G') {
                    map2[i][j] = 'R';
                } else map2[i][j] = charArray[j];
            }
        }

        visited = new boolean[N][N];
        int map1Count = 0;
        int map2Count = 0;

		// map1 적록색약이 아닌 사람이 볼 때의 그룹의 갯수
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                if (!visited[i][j]) {
                    map1Count++;
                    bfs1(new point(i, j));
                }
            }
        }

		// 방문 배열 초기화
        // map2 적록색약인 사람이 볼 때의 그룹의 갯수
        visited = new boolean[N][N];
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                if (!visited[i][j]) {
                    map2Count++;
                    bfs2(new point(i, j));
                }
            }
        }

        bw.write(map1Count + " " + map2Count);

        bw.flush();
        bw.close();
        br.close();
    }

    public static void bfs1(point start) {
        Queue<point> queue = new LinkedList<>();
        queue.add(start);
        visited[start.x][start.y] = true;

        while (!queue.isEmpty()) {
            point current = queue.poll();
            for (int i = 0; i < 4; i++) {
                int newX = current.x + xMove[i];
                int newY = current.y + yMove[i];

                if (newX < 0 || newY < 0 || newX >= N || newY >= N || visited[newX][newY]) continue;

                // 같은 색인지 확인
                if (map1[newX][newY] == map1[current.x][current.y]) {
                    visited[newX][newY] = true;
                    queue.add(new point(newX, newY));
                }
            }
        }
    }
    public static void bfs2(point start) {
        Queue<point> queue = new LinkedList<>();
        queue.add(start);
        visited[start.x][start.y] = true;

        while (!queue.isEmpty()) {
            point current = queue.poll();
            for (int i = 0; i < 4; i++) {
                int newX = current.x + xMove[i];
                int newY = current.y + yMove[i];

                if (newX < 0 || newY < 0 || newX >= N || newY >= N || visited[newX][newY]) continue;

                // 같은 색인지 확인
                if (map2[newX][newY] == map2[current.x][current.y]) {
                    visited[newX][newY] = true;
                    queue.add(new point(newX, newY));
                }
            }
        }
    }

    public static class point {
        int x;
        int y;

        public point(int x, int y) {
            this.x = x;
            this.y = y;
        }
    }
}
profile
가오리의 개발 이야기

0개의 댓글

관련 채용 정보