λ°˜μ‘ν˜•
Notice
Recent Posts
Recent Comments
Link
Β«   2025/03   Β»
일 μ›” ν™” 수 λͺ© 금 ν† 
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31
Archives
Today
Total
03-10 15:46
관리 메뉴

ImJay

[BOJ/Java] 6087. λ ˆμ΄μ € 톡신 λ³Έλ¬Έ

μ•Œκ³ λ¦¬μ¦˜/BOJ - Java

[BOJ/Java] 6087. λ ˆμ΄μ € 톡신

ImJay 2025. 3. 9. 03:33
λ°˜μ‘ν˜•

πŸ“Œ [BOJ/Java] 6087. λ ˆμ΄μ € 톡신

πŸ”— 문제 링크: λ°±μ€€ 6087번 - λ ˆμ΄μ € 톡신


πŸ“– 문제 해석

2차원 λ§΅μ—μ„œ 두 개의 λ ˆμ΄μ € πŸ”¦κ°€ 주어진닀.
λ ˆμ΄μ €λŠ” **.(빈 곡간)**μ—μ„œ 이동할 수 있으며, ***(λ²½)**을 톡과할 수 μ—†λ‹€.
이동할 λ•ŒλŠ” μƒν•˜μ’Œμš° λ°©ν–₯으둜만 κ°€λŠ₯ν•˜λ©°,
거울 (/ λ˜λŠ” \)을 μ„€μΉ˜ν•˜μ—¬ λΉ›μ˜ λ°©ν–₯을 λ³€κ²½ν•  수 μžˆλ‹€.

βœ… λͺ©ν‘œλŠ” μ‹œμž‘ λ ˆμ΄μ €μ—μ„œ 도착 λ ˆμ΄μ €κΉŒμ§€ μ΅œμ†Œν•œμ˜ κ±°μšΈμ„ μ„€μΉ˜ν•˜λŠ” 것이닀.


πŸ› οΈ 풀이 κ³Όμ •

이 λ¬Έμ œλŠ” BFS + λ‹€μ΅μŠ€νŠΈλΌ μ•Œκ³ λ¦¬μ¦˜μ„ μ‘μš©ν•˜μ—¬ ν•΄κ²°ν•  수 μžˆλ‹€.
즉, **μš°μ„ μˆœμœ„ 큐(μ΅œμ†Œ νž™)**λ₯Ό μ‚¬μš©ν•˜μ—¬ 거울 μ„€μΉ˜ νšŸμˆ˜κ°€ 적은 κ²½λ‘œλΆ€ν„° νƒμƒ‰ν•˜λŠ” 방식이닀.

1️⃣ μž…λ ₯ 처리 및 μ΄ˆκΈ°ν™”

  • W x H 크기의 맡 정보λ₯Ό μž…λ ₯λ°›κ³ , λ ˆμ΄μ €μ˜ μ‹œμž‘μ (C1)κ³Ό 도착점(C2)을 μ°ΎλŠ”λ‹€.
  • 4λ°©ν–₯(상, ν•˜, 쒌, 우)으둜 탐색을 μˆ˜ν–‰ν•˜λ©°, κ±°μšΈμ„ μ„€μΉ˜ν•΄μ•Όλ§Œ λ°©ν–₯을 λ°”κΏ€ 수 μžˆλ‹€.

2️⃣ μš°μ„ μˆœμœ„ 큐(BFS + λ‹€μ΅μŠ€νŠΈλΌ) ν™œμš©

  • (거울 μ„€μΉ˜ 개수, x μ’Œν‘œ, y μ’Œν‘œ, 진행 λ°©ν–₯)을 μ €μž₯ν•˜λŠ” **μš°μ„ μˆœμœ„ 큐(μ΅œμ†Œ νž™)**λ₯Ό μ‚¬μš©ν•œλ‹€.
  • 거울 μ„€μΉ˜ νšŸμˆ˜κ°€ 적은 κ²½μš°λΆ€ν„° νƒμƒ‰ν•˜μ—¬, 더 적은 거울 개수둜 λ„μ°©ν•˜λŠ” 경우λ₯Ό μ°ΎλŠ”λ‹€.
  • λ°©λ¬Έν•œ μœ„μΉ˜λŠ” λ°©ν–₯λ³„λ‘œ κΈ°λ‘ν•˜μ—¬ 쀑볡 탐색을 λ°©μ§€ν•œλ‹€.

3️⃣ μ΅œμ†Œ 거울 μ„€μΉ˜ 개수 μ°ΎκΈ°

  • 도착 지점(C2)에 λ„μ°©ν–ˆμ„ λ•Œμ˜ 거울 개수 쀑 μ΅œμ†Œκ°’μ„ 좜λ ₯ν•œλ‹€.

πŸ’» μ½”λ“œ κ΅¬ν˜„ (Java)

import java.util.*;

public class Main {
    static class Node implements Comparable<Node> {
        int x, y, dir, mirrors;

        public Node(int x, int y, int dir, int mirrors) {
            this.x = x;
            this.y = y;
            this.dir = dir;
            this.mirrors = mirrors;
        }

        @Override
        public int compareTo(Node o) {
            return this.mirrors - o.mirrors; // 거울 개수 κΈ°μ€€μœΌλ‘œ μš°μ„ μˆœμœ„ 큐 μ •λ ¬
        }
    }

    static int W, H;
    static char[][] map;
    static int[][][] visited;
    static int[] dx = { -1, 1, 0, 0 }; // 상, ν•˜, 쒌, 우
    static int[] dy = { 0, 0, -1, 1 };
    static List<int[]> lasers = new ArrayList<>();

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        W = sc.nextInt();
        H = sc.nextInt();
        map = new char[H][W];
        visited = new int[H][W][4]; // λ°©ν–₯별 λ°©λ¬Έ λ°°μ—΄ (μƒν•˜μ’Œμš°)

        for (int i = 0; i < H; i++) {
            String line = sc.next();
            for (int j = 0; j < W; j++) {
                map[i][j] = line.charAt(j);
                if (map[i][j] == 'C') {
                    lasers.add(new int[] { i, j }); // λ ˆμ΄μ € μœ„μΉ˜ μ €μž₯
                }
            }
        }

        System.out.println(bfs());
    }

    public static int bfs() {
        PriorityQueue<Node> pq = new PriorityQueue<>();
        int sx = lasers.get(0)[0], sy = lasers.get(0)[1];
        int ex = lasers.get(1)[0], ey = lasers.get(1)[1];

        for (int i = 0; i < 4; i++) {
            visited[sx][sy][i] = 0;
            pq.offer(new Node(sx, sy, i, 0)); // λͺ¨λ“  λ°©ν–₯μ—μ„œ μ‹œμž‘ κ°€λŠ₯
        }

        while (!pq.isEmpty()) {
            Node cur = pq.poll();

            if (cur.x == ex && cur.y == ey) return cur.mirrors; // λ„μ°©ν•˜λ©΄ μ’…λ£Œ

            for (int i = 0; i < 4; i++) {
                int nx = cur.x + dx[i];
                int ny = cur.y + dy[i];
                int newMirrors = cur.mirrors + (cur.dir == i ? 0 : 1); // λ°©ν–₯이 λ°”λ€Œλ©΄ 거울 μΆ”κ°€

                if (nx < 0 || ny < 0 || nx >= H || ny >= W || map[nx][ny] == '*') continue;
                if (visited[nx][ny][i] == 0 || visited[nx][ny][i] > newMirrors) {
                    visited[nx][ny][i] = newMirrors;
                    pq.offer(new Node(nx, ny, i, newMirrors));
                }
            }
        }

        return -1; // 도달 λΆˆκ°€λŠ₯ν•œ 경우
    }
}

⏱️ μ‹œκ°„ λ³΅μž‘λ„ 뢄석

  • BFS + λ‹€μ΅μŠ€νŠΈλΌ λ°©μ‹μ΄λ―€λ‘œ **O(W × H log(W × H))**의 μ‹œκ°„ λ³΅μž‘λ„λ₯Ό 가진닀.
  • 맡의 크기가 μ΅œλŒ€ 100 × 100 = 10,000 μ΄λ―€λ‘œ, log(W × H)λ₯Ό 고렀해도 μΆ©λΆ„νžˆ λΉ λ₯΄κ²Œ λ™μž‘ν•œλ‹€.
  • μš°μ„ μˆœμœ„ 큐λ₯Ό μ‚¬μš©ν•˜μ—¬ μ΅œμ†Œ 거울 κ°œμˆ˜λΆ€ν„° νƒμƒ‰ν•˜λ―€λ‘œ, λΆˆν•„μš”ν•œ 탐색을 쀄일 수 μžˆλ‹€.

πŸ’‘ λŠλ‚€μ 

βœ… λ‹¨μˆœν•œ BFSκ°€ μ•„λ‹Œ, μ΅œμ†Œν•œμ˜ 거울 μ„€μΉ˜ 개수λ₯Ό μš°μ„  νƒμƒ‰ν•˜λŠ” 방식을 κ³ λ―Όν•΄μ•Ό ν–ˆλ‹€.
βœ… **μš°μ„ μˆœμœ„ 큐(λ‹€μ΅μŠ€νŠΈλΌ)**λ₯Ό ν™œμš©ν•˜λ©΄ 더 효율적인 탐색이 κ°€λŠ₯ν•˜λ‹€λŠ” 점이 μ€‘μš”ν–ˆλ‹€.
βœ… λ°©ν–₯λ³„λ‘œ 방문을 κ΄€λ¦¬ν•˜λŠ” **3차원 λ°°μ—΄(visited[x][y][dir])**을 μ‚¬μš©ν•˜λŠ” 것이 ν•΅μ‹¬μ΄μ—ˆλ‹€.
βœ… λ ˆμ΄μ € 톡신 λ¬Έμ œλŠ” μ „ν˜•μ μΈ κ·Έλž˜ν”„ 탐색 문제둜, μœ μ‚¬ λ¬Έμ œμ—λ„ μ μš©ν•  수 μžˆμ„ 것 κ°™λ‹€!


🎯 이 문제λ₯Ό 톡해 BFS와 λ‹€μ΅μŠ€νŠΈλΌλ₯Ό κ²°ν•©ν•˜λŠ” 방법을 읡힐 수 μžˆμ—ˆλ‹€! πŸš€

λ°˜μ‘ν˜•
Comments