πŸ“– [λ°±μ€€μ•Œκ³ λ¦¬μ¦˜ 풀이] Q.2206 λ²½ λΆ€μˆ˜κ³  μ΄λ™ν•˜κΈ° 문제 풀이 - java

πŸ“– 문제

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

NΓ—M의 ν–‰λ ¬λ‘œ ν‘œν˜„λ˜λŠ” 맡이 μžˆλ‹€. λ§΅μ—μ„œ 0은 이동할 수 μžˆλŠ” 곳을 λ‚˜νƒ€λ‚΄κ³ , 1은 이동할 수 μ—†λŠ” 벽이 μžˆλŠ” 곳을 λ‚˜νƒ€λ‚Έλ‹€.

당신은 (1, 1)μ—μ„œ (N, M)의 μœ„μΉ˜κΉŒμ§€ μ΄λ™ν•˜λ € ν•˜λŠ”λ°, μ΄λ•Œ μ΅œλ‹¨ 경둜둜 μ΄λ™ν•˜λ € ν•œλ‹€. μ΅œλ‹¨κ²½λ‘œλŠ” λ§΅μ—μ„œ κ°€μž₯ 적은 개수의 칸을 μ§€λ‚˜λŠ” 경둜λ₯Ό λ§ν•˜λŠ”λ°,

μ΄λ•Œ μ‹œμž‘ν•˜λŠ” μΉΈκ³Ό λλ‚˜λŠ” 칸도 ν¬ν•¨ν•΄μ„œ μ„Όλ‹€.

λ§Œμ•½μ— μ΄λ™ν•˜λŠ” 도쀑에 ν•œ 개의 벽을 λΆ€μˆ˜κ³  μ΄λ™ν•˜λŠ” 것이 μ’€ 더 κ²½λ‘œκ°€ 짧아진닀면, 벽을 ν•œ 개 κΉŒμ§€ λΆ€μˆ˜κ³  μ΄λ™ν•˜μ—¬λ„ λœλ‹€.

ν•œ μΉΈμ—μ„œ 이동할 수 μžˆλŠ” 칸은 μƒν•˜μ’Œμš°λ‘œ μΈμ ‘ν•œ 칸이닀.

맡이 μ£Όμ–΄μ‘Œμ„ λ•Œ, μ΅œλ‹¨ 경둜λ₯Ό ꡬ해 λ‚΄λŠ” ν”„λ‘œκ·Έλž¨μ„ μž‘μ„±ν•˜μ‹œμ˜€.

πŸ” 접근법

Q.2206 λ²½ λΆ€μˆ˜κ³  μ΄λ™ν•˜κΈ° λ¬Έμ œμž…λ‹ˆλ‹€. (1,1) μ—μ„œ (N,M) 의 λͺ©μ μ§€ κΉŒμ§€ μ΄λ™ν•˜λ € ν•  λ–„

μ΅œλ‹¨ 경둜λ₯Ό κ΅¬ν•˜λŠ” λ¬Έμ œμž…λ‹ˆλ‹€.

이동할 λ•Œ κ°€μ€‘μΉ˜κ°€ μ—†λŠ” κ·Έλž˜ν”„μ΄λ―€λ‘œ μ΅œλ‹¨ 경둜λ₯Ό κ΅¬ν•˜κΈ° μœ„ν•΄

BFS 탐색을 톡해 ꡬ할 수 μžˆμŠ΅λ‹ˆλ‹€.

ν•˜μ§€λ§Œ, 이 λ¬Έμ œλŠ” 단 ν•œλ²ˆ 벽을 λΆ€μˆ˜κ³  갈 μˆ˜κ°€ μžˆμŠ΅λ‹ˆλ‹€.

κ·Έλž˜μ„œ 벽을 λΆ€μˆ˜κ³  κ°€λŠ” κ²½μš°μ™€ 벽을 λΆ€μˆ˜μ§€ μ•ŠλŠ” 경우λ₯Ό λ‚˜λˆ  풀이가 κ°€λŠ₯ν•©λ‹ˆλ‹€.

기쑴에 2차원 λ°°μ—΄λ‘œ visitedλ₯Ό ν™•μΈν–ˆλ‹€λ©΄

이제 3차원 배열을 톡해 벽을 λΆ€μˆœ κ²½μš°μ™€ μ•ˆ λΆ€μˆœ κ²½μš°κΉŒμ§€ ν™•μΈν•΄μ€λ‹ˆλ‹€.

bfs 탐색을 ν•˜μ—¬

νμ—μ„œ κΊΌλ‚Έ μœ„μΉ˜κ°€ (n,m) 이라면

ν•΄λ‹Ή μœ„μΉ˜μ˜ μ΅œλ‹¨κ²½λ‘œλ₯Ό 좜λ ₯ν•΄μ£Όλ©΄ λ©λ‹ˆλ‹€.

πŸ’» μ½”λ“œ

package problem.bfsdfs;
import java.io.*;
import java.util.LinkedList;
import java.util.Queue;

public class Main_2206 {
    static int n,m;
    static boolean [][][] visited;
    static int [][] maze;
    static int moveX[] = {0,0,-1,1};
    static int moveY[] = {-1,1,0,0};
    static int distance[][];

    public static void main(String[] args) throws IOException {
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        input();
        solve();
        bw.flush();
        bw.close();
    }
    public static void input() throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String [] input = br.readLine().split(" ");
        n = Integer.parseInt(input[0]);
        m = Integer.parseInt(input[1]);
        visited = new boolean[n][m][2];
        maze = new int[n][m];
        distance = new int[n][m];

        for(int i=0; i<n; i++){
            String temp[] = br.readLine().split("");
            for(int j=0; j<m; j++){
                maze[i][j] = Integer.parseInt(temp[j]);
            }
        }
    }

    public static void solve () {
        Queue<int[]> queue = new LinkedList();
        queue.offer(new int[]{0, 0,0});
        visited[0][0][0] = true;
        distance[0][0] = 1;

        while (!queue.isEmpty()){
            int coordinate[] = queue.poll();
            if(coordinate[0] == n-1 && coordinate[1] == m-1 ){
                System.out.println(distance[coordinate[0]][coordinate[1]]);
                return;
            }

            for(int i=0; i<4; i++){
                int x = coordinate[0] + moveX[i];
                int y = coordinate[1] + moveY[i];
                int num = coordinate[2];

                if ( x < 0 || y< 0 || x>=n || y>=m) {
                    continue;
                }

                if ( maze[x][y] == 1 ){
                    if( num==0 && !visited[x][y][1] ){
                        queue.offer(new int[]{x,y,1});
                        visited[x][y][1] = true;
                        distance[x][y] = distance[coordinate[0]][coordinate[1]] +1;
                    }
                }
                else {
                    if(visited[x][y][num]) {
                        continue;
                    }
                    queue.offer(new int[]{x,y,num});
                    visited[x][y][num] = true;
                    distance[x][y] = distance[coordinate[0]][coordinate[1]] +1;
                }
            }
        }
        System.out.println(-1);
    }
}
Written on March 29, 2021