πŸ“– [λ°±μ€€μ•Œκ³ λ¦¬μ¦˜ 풀이] Q.1261 μ•Œκ³ μŠ€νŒŸ 문제 풀이 - java

πŸ“– 문제

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

μ•Œκ³ μŠ€νŒŸ μš΄μ˜μ§„μ΄ λͺ¨λ‘ λ―Έλ‘œμ— κ°‡ν˜”λ‹€. λ―Έλ‘œλŠ” NM 크기이며, 총 11크기의 방으둜 이루어져 μžˆλ‹€. λ―Έλ‘œλŠ” 빈 λ°© λ˜λŠ” 벽으둜 이루어져 있고, 빈 방은 자유둭게 닀닐 수 μžˆμ§€λ§Œ, 벽은 λΆ€μˆ˜μ§€ μ•ŠμœΌλ©΄ 이동할 수 μ—†λ‹€.

μ•Œκ³ μŠ€νŒŸ μš΄μ˜μ§„μ€ μ—¬λŸ¬λͺ…μ΄μ§€λ§Œ, 항상 λͺ¨λ‘ 같은 방에 μžˆμ–΄μ•Ό ν•œλ‹€. 즉, μ—¬λŸ¬ λͺ…이 λ‹€λ₯Έ 방에 μžˆμ„ μˆ˜λŠ” μ—†λ‹€. μ–΄λ–€ λ°©μ—μ„œ 이동할 수 μžˆλŠ” 방은 μƒν•˜μ’Œμš°λ‘œ μΈμ ‘ν•œ 빈 방이닀. 즉, ν˜„μž¬ μš΄μ˜μ§„μ΄ (x, y)에 μžˆμ„ λ•Œ, 이동할 수 μžˆλŠ” 방은 (x+1, y), (x, y+1), (x-1, y), (x, y-1) 이닀. 단, 미둜의 λ°–μœΌλ‘œ 이동 ν•  μˆ˜λŠ” μ—†λ‹€.

벽은 ν‰μ†Œμ—λŠ” 이동할 수 μ—†μ§€λ§Œ, μ•Œκ³ μŠ€νŒŸμ˜ 무기 AOJλ₯Ό μ΄μš©ν•΄ 벽을 λΆ€μˆ˜μ–΄ 버릴 수 μžˆλ‹€. 벽을 λΆ€μˆ˜λ©΄, 빈 λ°©κ³Ό λ™μΌν•œ 방으둜 λ³€ν•œλ‹€.

λ§Œμ•½ 이 λ¬Έμ œκ°€ μ•Œκ³ μŠ€νŒŸμ— μžˆλ‹€λ©΄, μš΄μ˜μ§„λ“€μ€ ꢁ극의 무기 sudoλ₯Ό μ΄μš©ν•΄ 벽을 ν•œ λ²ˆμ— λ‹€ 없애버릴 수 μžˆμ§€λ§Œ, μ•ˆνƒ€κΉκ²Œλ„ 이 λ¬Έμ œλŠ” Baekjoon Online Judge에 μˆ˜λ‘λ˜μ–΄ 있기 λ•Œλ¬Έμ—, sudoλ₯Ό μ‚¬μš©ν•  수 μ—†λ‹€.

ν˜„μž¬ (1, 1)에 μžˆλŠ” μ•Œκ³ μŠ€νŒŸ μš΄μ˜μ§„μ΄ (N, M)으둜 μ΄λ™ν•˜λ €λ©΄ 벽을 μ΅œμ†Œ λͺ‡ 개 λΆ€μˆ˜μ–΄μ•Ό ν•˜λŠ”μ§€ κ΅¬ν•˜λŠ” ν”„λ‘œκ·Έλž¨μ„ μž‘μ„±ν•˜μ‹œμ˜€.

πŸ” 접근법

BOJ Q.1261 μ•Œκ³ μŠ€νŒŸ λ¬Έμ œμž…λ‹ˆλ‹€. N*M 크기의 λ―Έλ‘œκ°€ 주어지고 λ―Έλ‘œλŠ” 빈 λ°© λ˜λŠ” 벽으둜 이루어져 μžˆμŠ΅λ‹ˆλ‹€.

빈 방은 κ·Έλƒ₯ μ§€λ‚˜λ‹€λ‹ 수 있고 벽은 λΆ€μˆ˜κ³  이동이 κ°€λŠ₯ν•©λ‹ˆλ‹€.

이동은 μΈμ ‘ν•œ λ™μ„œλ‚¨λΆμœΌλ‘œλ§Œ κ°€λŠ₯ν•©λ‹ˆλ‹€.

1,1 μ§€μ μ—μ„œ μΆœλ°œν•œλ‹€κ³  ν•  λ•Œ N,MκΉŒμ§€ 이동을 ν•˜λ €κ³  ν•˜λŠ”λ° μ΄λ•Œ 벽을 μ΅œμ†Œ λͺ‡ 개λ₯Ό λΆ€μˆ΄μ•Όν•˜λŠ”μ§€λ₯Ό κ΅¬ν•˜λŠ” λ¬Έμ œμž…λ‹ˆλ‹€.

문제λ₯Ό 보면 N*M 크기의 λ―Έλ‘œκ°€ 주어지고 λ™μ„œλ‚¨λΆμœΌλ‘œ 이동이 κ°€λŠ₯ν•˜λ‹ˆ

일반적인 bfs νƒμƒ‰μœΌλ‘œ 풀이가 κ°€λŠ₯ν•΄λ³΄μž…λ‹ˆλ‹€.

ν•˜μ§€λ§Œ, 이 λ¬Έμ œμ—μ„œ 핡심인 κ°€μ€‘μΉ˜κ°€ 이동거리라면 일반적인 bfs νƒμƒ‰μœΌλ‘œ 풀이가 κ°€λŠ₯ν–ˆκ² μ§€λ§Œ

μ—¬κΈ°μ„œ κ°€μ€‘μΉ˜λŠ” 이동거리가 μ•„λ‹Œ 벽을 λΆ€μˆ˜λŠ” κ°―μˆ˜μž…λ‹ˆλ‹€.

벽이 μžˆλƒ 없냐에 따라 λΆ€μˆ˜κ³  이동할 μˆ˜λ„ 있고 (1) κ·Έλƒ₯ 이동할 μˆ˜λ„ μžˆμŠ΅λ‹ˆλ‹€. (0)

κ°€μ€‘μΉ˜κ°€ 1이 μ•„λ‹Œ 1 λ˜λŠ” 0인 κ·Έλž˜ν”„λ₯Ό 탐색해야 ν•˜λ―€λ‘œ

λ‹€μ΅μŠ€νŠΈλΌ μ•Œκ³ λ¦¬μ¦˜μ„ μ΄μš©ν•΄ 풀이가 κ°€λŠ₯ν•©λ‹ˆλ‹€.

λ³΄ν†΅μ˜ μ΅œλ‹¨ 경둜λ₯Ό ꡬ할 λ•Œ 처럼 λ‹€μ΅μŠ€νŠΈλΌλ₯Ό μœ„ν•œ ν…Œμ΄λΈ”μ„ λ§Œλ“­λ‹ˆλ‹€.

그리고 λŒ€μ‹ , 거리λ₯Ό μ€‘μ‹¬μœΌλ‘œ λ³΄λŠ” 것이 μ•„λ‹ˆλΌ 벽을 λΆ€μˆ˜λŠ” 것을 μ€‘μ‹¬μœΌλ‘œ

ν˜„μž¬ μœ„μΉ˜μ—μ„œ 각 λ‹€μŒ μœ„μΉ˜(λ™μ„œλ‚¨λΆ)둜 이동할 λ•Œλ§ˆλ‹€ 더 μ΅œμ†Œν•œμœΌλ‘œ 벽을 λΆ€μˆ˜κ³  이동할 수 μžˆλ‹€λ©΄ κ°±μ‹ ν•΄μ£Όλ©΄ λ©λ‹ˆλ‹€.

if(distance[x][y] > distance[currentNode.x][currentNode.y]+maze[x][y] ){
                    distance[x][y] = distance[currentNode.x][currentNode.y]+maze[x][y];
                    priorityQueue.add(new Node(x,y,distance[x][y]));
}

πŸ’» μ½”λ“œ


package problem.shortest.Q1261;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.PriorityQueue;

public class Main_1261 {

    static int moveX[] = {0,0,-1,1};
    static int moveY[] = {-1,1,0,0};
    static int maze[][];
    static int distance[][];
    static int n,m;

    static class Node implements Comparable<Node>{
        int x;
        int y;
        int distance;

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

        @Override
        public int compareTo(Node target) {
            // μž‘μ€ 거리 λΉ„μš©μ΄ λ¨Όμ € μ˜€λ„λ‘
            return this.distance - target.distance;
        }
    }

    public static void main(String[] args) throws IOException {
        input();
        solve();
        System.out.println(distance[n-1][m-1]);
    }

    private static void input() throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String temp[] = br.readLine().split( " ");
        m= Integer.parseInt(temp[0]);
        n= Integer.parseInt(temp[1]);
        maze = new int[n][m];
        distance = new int[n][m];
        for(int i=0; i<n; i++){
            Arrays.fill(distance[i],Integer.MAX_VALUE);
        }

        for(int i=0; i<n; i++){
            temp = br.readLine().split("");
            for(int j=0; j<m; j++){
                maze[i][j] = Integer.parseInt(temp[j]);
            }
        }
        br.close();
    }
    private static void solve(){
        PriorityQueue<Node> priorityQueue = new PriorityQueue<>();
        priorityQueue.add(new Node(0,0,0));
        distance[0][0] = 0;

        while (! priorityQueue.isEmpty()){
            Node currentNode = priorityQueue.poll();
            if(currentNode.x == n-1 && currentNode.y == m-1){
                return;
            }

            for(int i=0; i<4; i++){
                int x = currentNode.x +moveX[i];
                int y = currentNode.y + moveY[i];
                if(x < 0 || y <0 || x >= n || y >= m){
                    continue;
                }
                if(distance[x][y] > distance[currentNode.x][currentNode.y]+maze[x][y] ){
                    distance[x][y] = distance[currentNode.x][currentNode.y]+maze[x][y];
                    priorityQueue.add(new Node(x,y,distance[x][y]));
                }
            }
        }
    }
}

Written on April 21, 2021