๊ธฐ๋ก๋ฐฉ

BOJ_13459 : ๊ตฌ์Šฌ ํƒˆ์ถœ ๋ณธ๋ฌธ

CodingTest/Java

BOJ_13459 : ๊ตฌ์Šฌ ํƒˆ์ถœ

Soom_1n 2023. 4. 6. 17:54

๐Ÿ‘‰ ๋ฌธ์ œ๋งํฌ

 

13459๋ฒˆ: ๊ตฌ์Šฌ ํƒˆ์ถœ

์ฒซ ๋ฒˆ์งธ ์ค„์—๋Š” ๋ณด๋“œ์˜ ์„ธ๋กœ, ๊ฐ€๋กœ ํฌ๊ธฐ๋ฅผ ์˜๋ฏธํ•˜๋Š” ๋‘ ์ •์ˆ˜ N, M (3 ≤ N, M ≤ 10)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‹ค์Œ N๊ฐœ์˜ ์ค„์— ๋ณด๋“œ์˜ ๋ชจ์–‘์„ ๋‚˜ํƒ€๋‚ด๋Š” ๊ธธ์ด M์˜ ๋ฌธ์ž์—ด์ด ์ฃผ์–ด์ง„๋‹ค. ์ด ๋ฌธ์ž์—ด์€ '.', '#', 'O', 'R', 'B'

www.acmicpc.net


< ์˜ˆ์ œ ์ƒ๋žต >


๐Ÿ”ธ ๋ฌธ์ œ ๋ถ„์„ ๐Ÿ”ธ

  • N x M ๋ณด๋“œํŒ์— ๋ชจ์„œ๋ฆฌ๋Š” ๋ง‰ํ˜€์žˆ๊ณ , ๋นจ๊ฐ„ ๊ณต : R, ํŒŒ๋ž€ ๊ณต : B, ๊ตฌ๋ฉ : O ๊ฐ€ ์ฃผ์–ด์ง„๋‹ค.
    • ๋ณด๋“œํŒ์„ ๊ธฐ์šธ์ด๋ฉด ๋‘ ๊ณต์ด ์›€์ง์ผ ์ˆ˜ ์žˆ๋Š” ๊ณต๊ฐ„ ๋๊นŒ์ง€ ํ•œ์ชฝ์œผ๋กœ ์ด๋™ํ•œ๋‹ค.
    • ๋นจ๊ฐ„ ๊ณต ๋งŒ ๊ตฌ๋ฉ์œผ๋กœ ๋บ„ ์ˆ˜ ์žˆ์œผ๋ฉด 1, ์—†์œผ๋ฉด 0 ์„ ์ถœ๋ ฅํ•œ๋‹ค.
  • ๋ณด๋“œํŒ์„ ๊ธฐ์šธ์ด๋Š” 4๊ฐ€์ง€ ์„ ํƒ์— ๋”ฐ๋ฅธ ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•ด์•ผ ํ•˜๋ฏ€๋กœ BFS๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.
  • ๊ณต์˜ ๊ตด๋Ÿฌ๊ฐ, ๊ฒŒ์ž„ ์ข…๋ฃŒ ํ™•์ธ์„ ์‹œ๋ฎฌ๋ ˆ์ด์…˜ํ•œ๋‹ค.

๐Ÿ”ธ ์ฝ”๋“œ ๐Ÿ”ธ

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.Queue;
import java.util.StringTokenizer;

public class Main {
    private static char[][] map;
    private static int[] dx = {-1, 0, 1, 0};
    private static int[] dy = {0, 1, 0, -1};

    public static void main(String[] args) throws IOException {
        // Input
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        int N = Integer.parseInt(st.nextToken());
        int M = Integer.parseInt(st.nextToken());
        map = new char[N][M];
        int[] start = new int[4];
        for (int i = 0; i < N; i++) {
            String s = br.readLine();
            for (int j = 0; j < M; j++) {
                char c = s.charAt(j);
                if (c == 'R') {
                    start[0] = i;
                    start[1] = j;
                    map[i][j] = '.';
                } else if (c == 'B') {
                    start[2] = i;
                    start[3] = j;
                    map[i][j] = '.';
                } else {
                    map[i][j] = c;
                }
            }
        }

        // BFS + Output
        System.out.println(bfs(start));
    }

    private static int bfs(int[] start) {
        Queue<int[]> que = new ArrayDeque<>();
        que.add(start);
        int cnt = 1;
        while (!que.isEmpty() && cnt <= 10) {
            int size = que.size();
            for (int i = 0; i < size; i++) {
                int[] points = que.poll();
                for (int j = 0; j < 4; j++) {
                    int[] result = new int[4];      // ๊ตด๋ ธ์„๋•Œ ๊ณต ์œ„์น˜
                    int ret = go(j, points, result);   // ๊ณต ๊ตด๋ ค๋ณด๊ธฐ
                    if (ret == 1) return 1;         // ์ •๋‹ต ์ฐพ์Œ
                    else if (ret == 0) {            // ์ง„ํ–‰์€ ๊ฐ€๋Šฅ
                        que.add(result);
                    }
                }
            }
            cnt++;
        }
        return 0;
    }

    private static int go(int d, int[] points, int[] result) {
        boolean red = false, blue = false;
        int rx = points[0];
        int ry = points[1];
        int bx = points[2];
        int by = points[3];

//        System.out.println(rx + " " + ry + " " + bx + " " + by + " " + d);

        // ๋นจ๊ฐ„ ๊ณต ๊ตด๋ฆฌ๊ธฐ
        while (map[rx + dx[d]][ry + dy[d]] != '#') {
            rx += dx[d];
            ry += dy[d];
            if (map[rx][ry] == 'O') {
                red = true;
                break;
            }
//            System.out.println(rx + " " + ry);
        }

        // ํŒŒ๋ž€ ๊ณต ๊ตด๋ฆฌ๊ธฐ
        while (map[bx + dx[d]][by + dy[d]] != '#') {
            bx += dx[d];
            by += dy[d];
            if (map[bx][by] == 'O') {
                blue = true;
                break;
            }
        }

        if (blue) return 2;         // ํŒŒ๋ž€๊ณต ๋“ค์–ด๊ฐ€๋ฒ„๋ฆผ (์ง„ํ–‰๋ถˆ๊ฐ€)
        else if (red) return 1;     // ๋นจ๊ฐ„ ๊ณต๋งŒ ๋“ค์–ด๊ฐ (์„ฑ๊ณต)
        else {                      // ๋‘˜ ๋‹ค ์•ˆ๋“ค์–ด๊ฐ (๊ณ„์† ์ง„ํ–‰)
            if (rx == bx && ry == by) {  // ๋‘ ๊ณต์ด ๊ฒน์นจ
                if (first(d, points)) {      // ๋นจ๊ฐ„ ๊ณต์ด ๋น ๋ฆ„
                    bx += dx[(d + 2) % 4];
                    by += dy[(d + 2) % 4];
                } else {                    // ํŒŒ๋ž€ ๊ณต์ด ๋น ๋ฆ„
                    rx += dx[(d + 2) % 4];
                    ry += dy[(d + 2) % 4];
                }
            }
            result[0] = rx;
            result[1] = ry;
            result[2] = bx;
            result[3] = by;
            return 0;
        }
    }

    private static boolean first(int d, int[] points) {
        if (d == 0) {
            return points[0] < points[2];
        } else if (d == 1) {
            return points[1] > points[3];
        } else if (d == 2) {
            return points[0] > points[2];
        } else {
            return points[1] < points[3];
        }
    }
}

๐Ÿ”ธ ์ฝ”๋“œ ํ•ด์„ ๐Ÿ”ธ

  • ๋ณด๋“œํŒ ์ •๋ณด๋ฅผ ์ž…๋ ฅ ๋ฐ›์„ ๋•Œ ๊ณต๋“ค์˜ ์ขŒํ‘œ๋ฅผ start ๋ฐฐ์—ด์— ์ €์žฅํ•˜๊ณ  ๊ทธ ์ž๋ฆฌ๋Š” ๋นˆ ๊ณต๊ฐ„ '.'์œผ๋กœ ์ €์žฅํ•œ๋‹ค.
  • BFS๋ฅผ ๋Œ๋ ค ๋นจ๊ฐ„๊ณต๋งŒ ๋น ์ ธ๋‚˜์˜ฌ ์ˆ˜ ์žˆ๋Š”์ง€ ํŒ๋‹จํ•œ๋‹ค.
    • ๊ฒฉ์žํŒ์„ ๊ธฐ์šธ์ด๋Š” ๋ฐฉํ–ฅ์— ๋”ฐ๋ผ ๋‹ฌ๋ฆฌ์ง€๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ํ์— ๋‹ด์•„๊ฐ€๋ฉฐ ๊ณ„์‚ฐํ•œ๋‹ค.
    • ๊ฒฉ์žํŒ์„ ๊ธฐ์šธ์ธ ํ›„ ๊ณต์˜ ์œ„์น˜๋‚˜ ๊ฒฐ๊ณผ๋Š” go ๋ฉ”์„œ๋“œ์—์„œ ๊ตฌํ•œ๋‹ค.
      • while๋ฌธ์œผ๋กœ ๋‘ ๊ณต์„ ๋๊นŒ์ง€ ๋Œ๋ ค๋ณด๊ณ  ํŒŒ๋ž€๊ณต์ด ๋“ค์–ด๊ฐ”์œผ๋ฉด ์ง„ํ–‰๋ถˆ๊ฐ€, ๋นจ๊ฐ„๊ณต๋งŒ ๋“ค์–ด๊ฐ”์œผ๋ฉด ์„ฑ๊ณต์œผ๋กœ ํŒ๋‹จํ•œ๋‹ค.
      • ๋งŒ์•ฝ ๋‘ ๊ณต์ด ๊ฒน์ณค์œผ๋ฉด ๋ฐฉํ–ฅ์— ๋”ฐ๋ผ ๋จผ์ € ์ด๋™ํ•œ ๊ณต์„ first๋ฉ”์„œ๋“œ๋กœ ์ฐพ์•„ ๋’ค๋Šฆ๊ฒŒ ์›€์ง์ธ ๊ณต์„ ํ•œ ์นธ ๋’ค๋กœ ๋Œ๋ฆฐ๋‹ค.
      • ์ง„ํ–‰๋ถˆ๊ฐ€๋Š” 2, ์„ฑ๊ณต์€ 1, ๊ณ„์† ์ง„ํ–‰ํ•ด ๋ด์•ผ ๋˜๋ฉด 0์„ ๋ฆฌํ„ดํ•œ๋‹ค.
    • ์„ฑ๊ณตํ•˜๋ฉด bfs ๋ฉ”์„œ๋“œ๋ฅผ ์ข…๋ฃŒํ•˜๊ณ , ๊ณ„์† ์ง„ํ–‰ํ•ด์•ผ ๋˜๋ฉด ํ์— ์ขŒํ‘œ๋ฅผ ๋„ฃ๋Š”๋‹ค.
  • ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

๐Ÿ”ธ end ๐Ÿ”ธ

  • ์ฒ˜์Œ์—” dfs๋กœ ์ ‘๊ทผํ–ˆ๋‹ค๊ฐ€ ์ฝ”๋“œ๊ฐ€ ๋„ˆ๋ฌด ๋ณต์žกํ•ด์ ธ์„œ bfs๋กœ ๋‹ค์‹œ ํ•œ ๋ฒˆ ํ’€์—ˆ๋‹ค.
    • ๊ทธ๋ž˜๋„ ํ‹€๋ฆฌ๋Š” ๋ถ€๋ถ„์ด ์žˆ์–ด์„œ ์งˆ๋ฌธ ๊ฒŒ์‹œํŒ์„ ์ฐธ๊ณ ํ–ˆ๋‹ค.
    • ๊ตฌ์Šฌ ํƒˆ์ถœ 2์˜ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋ฅผ ๋Œ๋ ค๋ณด๊ณ  ํ‹€๋ฆฐ ์ ์„ ๋ฐœ๊ฒฌํ–ˆ๋Š”๋ฐ, bfs์˜ while๋ฌธ์„ ๋“ค์–ด๊ฐ€๊ธฐ ์ „ cnt์˜ ์ดˆ๊ธฐ๊ฐ’์„ 1์ด ์•„๋‹Œ 0์œผ๋กœ ์ค˜์„œ ์˜ค๋‹ต์ด ๋‚˜์™”์—ˆ๋‹ค.
      • cnt๋Š” ๋ณด๋“œํŒ์„ ๊ธฐ์šธ์ธ ํšŸ์ˆ˜์ด๋‹ค.

728x90

'CodingTest > Java' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

BOJ_16236 : ์•„๊ธฐ ์ƒ์–ด  (0) 2023.04.07
BOJ_14890 : ๊ฒฝ์‚ฌ๋กœ  (0) 2023.04.07
BOJ_2239 : ์Šค๋„์ฟ   (0) 2023.04.06
BOJ_17141 : ์—ฐ๊ตฌ์†Œ 2  (0) 2023.04.06
BOJ_2636 : ์น˜์ฆˆ  (0) 2023.04.06