๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ”Ž Algorithm/๐Ÿ’ป ๋ฌธ์ œ ํ’€์ด

[๋ฐฑ์ค€] - 2206. ๋ฒฝ๋ถ€์ˆ˜๊ณ  ์ด๋™ํ•˜๊ธฐ

by Seongpyo Hong 2021. 3. 24.

๋ฌธ์ œ

 

ํ’€์ด

์ด ๋ฌธ์ œ๋Š” ์˜ˆ์ „์— BFS๋ฅผ ํ•œ์ฐฝ ํ’€์—ˆ์„ ๋•Œ์—๋Š” ๋ชป ํ’€์—ˆ๋˜ ๋ฌธ์ œ์˜€๋Š”๋ฐ ์ด์™€ ์œ ์‚ฌํ•œ ๋ฌธ์ œ์ธ 1600. ๋ง์ด ๋˜๊ณ ํ”ˆ ์›์ˆญ์ด๋ฌธ์ œ๋ฅผ ํ’€๊ณ  ์˜๊ฐ์„ ์–ป์–ด์„œ ๋‹ค์‹œ ๋„์ „ํ–ˆ๋”๋‹ˆ ์‰ฝ๊ฒŒ ํ’€๋ ธ๋‹ค. 

์ด ๋ฌธ์ œ๊ฐ€ ์ผ๋ฐ˜์ ์ธ BFS๋ณด๋‹ค ๊นŒ๋‹ค๋กœ์šด ์ด์œ ๋Š” ์–ด๋–ค ์ขŒํ‘œ์— ์˜ฌ ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜๊ฐ€ 2๊ฐ€์ง€(์ด์ „์— ๋ฒฝ์„ ๋ถ€์‹  ๊ฒฝ์šฐ, ์ด์ „์— ๋ฒฝ์„ ๋ถ€์ˆ˜์ง€ ์•Š์€ ๊ฒฝ์šฐ)๋ผ๋Š” ์ ์ด๋‹ค. ๋”ฐ๋ผ์„œ 2์ฐจ์› ๋ฐฉ๋ฌธ ๋ฐฐ์—ด๋กœ๋Š” ์ฒ˜๋ฆฌ๊ฐ€ ๋ถˆ๊ฐ€๋Šฅํ•˜๊ณ  3์ฐจ์› ๋ฐฐ์—ด์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜์˜€๋‹ค. 3์ฐจ์› ๋ฐฐ์—ด์—์„œ 0๋ฒˆ์จฐ ์ธ๋ฑ์Šค๋Š” ๋ฒฝ์„ ์ด๋ฏธ ๋ถ€์ˆ˜๊ณ  ์˜จ ๊ฒฝ์šฐ, 1๋ฒˆ์งธ ์ธ๋ฑ์Šค๋Š” ๋ฒฝ์„ ์•„์ง ๋ถ€์ˆ˜์ง€ ์•Š์€ ๊ฒฝ์šฐ์ด๋‹ค.

bfs๋ฅผ ์ง„ํ–‰ํ•˜๋ฉฐ ๋‹ค์Œ์˜ 3๊ฐ€์ง€ ๊ฒฝ์šฐ์— ๋Œ€ํ•ด์„œ ์ฒ˜๋ฆฌํ•ด์ฃผ์—ˆ๋‹ค.

  1. ํ˜„์žฌ ๋ฒฝ์„ ๋ถ€์ˆ  ์ˆ˜ ์žˆ๋Š” ๊ธฐํšŒ๊ฐ€ ๋‚จ์•„์žˆ๊ณ  ๋‹ค์Œ ๋ฐฉ๋ฌธํ•  ์ขŒํ‘œ๊ฐ€ ๋ฒฝ์ธ ๊ฒฝ์šฐ, ๊ธฐํšŒ๋ฅผ ์†Œ์ง„ํ•˜๊ณ  ๋ฒฝ์„ ๋ฐฉ๋ฌธ
  2. ๊ธฐํšŒ๊ฐ€ ๋‚จ์•„์žˆ์ง€๋งŒ ๋‹ค์Œ ๋ฐฉ๋ฌธํ•  ์ขŒํ‘œ๊ฐ€ ๋ฒฝ์ด ์•„๋‹Œ ๊ฒฝ์šฐ์—๋Š” ๊ธฐํšŒ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ๋‹ค์Œ ์ง€์ ์œผ๋กœ ๋ฐฉ๋ฌธ
  3. ๊ธฐํšŒ๊ฐ€ ๋‚จ์•„์žˆ์ง€ ์•Š๋‹ค๋ฉด ๋‹ค์Œ ๋ฐฉ๋ฌธํ•  ์ขŒํ‘œ๊ฐ€ ๋ฒฝ์ด ์•„๋‹Œ ๊ฒฝ์šฐ๋งŒ ๋ฐฉ๋ฌธ

๊ตฌํ˜„ํ•œ ์ฝ”๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;

public class Solution2206 {
    static class Pair {
        int x;
        int y;
        int dist;
        int remain;

        public Pair(int x, int y, int dist, int remain) {
            this.x = x;
            this.y = y;
            this.dist = dist;
            this.remain = remain;
        }
    }
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static int N, M;
    static int[] dx = {1, -1, 0, 0}, dy = {0, 0, 1, -1};
    static int[][] map;
    static boolean[][][] visited;
    public static void main(String[] args) throws IOException {
        String[] input = br.readLine().split(" ");
        N = Integer.parseInt(input[0]);
        M = Integer.parseInt(input[1]);
        map = new int[N][M];
        visited = new boolean[N][M][2];
        for (int i = 0; i < N; i++) {
            String line = br.readLine();
            for (int j = 0; j < M; j++) {
                map[i][j] = Character.getNumericValue(line.charAt(j));
            }
        }

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

    private static int bfs() {
        Queue<Pair> q = new LinkedList<>();
        q.add(new Pair(0, 0, 1, 1));
        while (!q.isEmpty()) {
            Pair cur = q.poll();
            if (cur.x == N - 1 && cur.y == M - 1) return cur.dist;
            if (cur.remain == 1) {
                for (int i = 0; i < 4; i++) {
                    int nX = cur.x + dx[i];
                    int nY = cur.y + dy[i];
                    if (nX < 0 || nY < 0 || nX >= N || nY >= M) continue;
                    if (map[nX][nY] == 1 && !visited[nX][nY][0]) {
                        visited[nX][nY][0] = true;
                        q.add(new Pair(nX, nY, cur.dist + 1, 0));
                    } else if (map[nX][nY] == 0 && !visited[nX][nY][1]) {
                        visited[nX][nY][1] = true;
                        q.add(new Pair(nX, nY, cur.dist + 1, 1));
                    }
                }
            }

            for (int i = 0; i < 4; i++) {
                int nX = cur.x + dx[i];
                int nY = cur.y + dy[i];
                if (nX < 0 || nY < 0 || nX >= N || nY >= M || visited[nX][nY][cur.remain] || map[nX][nY] == 1) continue;
                visited[nX][nY][cur.remain] = true;
                q.add(new Pair(nX, nY, cur.dist + 1, cur.remain));
            }
        }

        return -1;
    }
}

๋Œ“๊ธ€