[Algorithm - Baekjoon] 16933번 벽 부수고 이동하기 3

nunu·2023년 9월 6일
0

Algorithm

목록 보기
72/142

문제

N×M의 행렬로 표현되는 맵이 있다. 맵에서 0은 이동할 수 있는 곳을 나타내고, 1은 이동할 수 없는 벽이 있는 곳을 나타낸다. 당신은 (1, 1)에서 (N, M)의 위치까지 이동하려 하는데, 이때 최단 경로로 이동하려 한다. 최단경로는 맵에서 가장 적은 개수의 칸을 지나는 경로를 말하는데, 이때 시작하는 칸과 끝나는 칸도 포함해서 센다. 이동하지 않고 같은 칸에 머물러있는 경우도 가능하다. 이 경우도 방문한 칸의 개수가 하나 늘어나는 것으로 생각해야 한다.

이번 문제에서는 낮과 밤이 번갈아가면서 등장한다. 가장 처음에 이동할 때는 낮이고, 한 번 이동할 때마다 낮과 밤이 바뀌게 된다. 이동하지 않고 같은 칸에 머무르는 경우에도 낮과 밤이 바뀌게 된다.

만약에 이동하는 도중에 벽을 부수고 이동하는 것이 좀 더 경로가 짧아진다면, 벽을 K개 까지 부수고 이동하여도 된다. 단, 벽은 낮에만 부술 수 있다.

한 칸에서 이동할 수 있는 칸은 상하좌우로 인접한 칸이다.

맵이 주어졌을 때, 최단 경로를 구해 내는 프로그램을 작성하시오.

입력

첫째 줄에 N(1 ≤ N ≤ 1,000), M(1 ≤ M ≤ 1,000), K(1 ≤ K ≤ 10)이 주어진다. 다음 N개의 줄에 M개의 숫자로 맵이 주어진다. (1, 1)과 (N, M)은 항상 0이라고 가정하자.

출력

첫째 줄에 최단 거리를 출력한다. 불가능할 때는 -1을 출력한다.

예제1 - 입력

1 4 1
0010

예제1 - 출력

5

예제2 - 입력

1 4 1
0100

예제2 - 출력

4

예제3 - 입력

6 4 1
0100
1110
1000
0000
0111
0000

예제3 - 출력

15

예제4 - 입력

6 4 2
0100
1110
1000
0000
0111
0000

예제4 - 출력

9

제출 코드

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

public class Main {
    public static void main(String[] args) throws IOException {
        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());
        int k = Integer.parseInt(st.nextToken());

        boolean[][] map = new boolean[n][m];
        for (int i = 0; i < n; i++) {
            char[] input = br.readLine().toCharArray();
            for (int j = 0; j < m; j++) {
                map[i][j] = input[j] == '0' ? false : true;
            }
        }

        boolean[][][] visited = new boolean[n][m][k + 1];
        Block now = new Block(0, 0, true, 1, 0);

        Queue<Block> queue = new LinkedList<>();
        queue.offer(now);
        visited[now.x][now.y][0] = true;

        int[] mx = {0, 0, -1, 1};
        int[] my = {-1, 1, 0, 0};
        while (!queue.isEmpty()) {
            now = queue.poll();
            if (now.x == n - 1 && now.y == m - 1) {
                System.out.println(now.dist);
                return;
            }

            for (int i = 0; i < mx.length; i++) {
                int tx = now.x + mx[i];
                int ty = now.y + my[i];

                if (tx < 0 || tx >= n || ty < 0 || ty >= m)
                    continue;

                int wall = now.wall;
                int dist = now.dist;
                boolean day = now.day;

                if (map[tx][ty]) {          //벽일 경우
                    if (day && (wall + 1 <= k) && !visited[tx][ty][wall + 1]) {        //벽 깨도 됨
                        queue.offer(new Block(tx, ty, false, dist + 1, wall + 1));
                        visited[tx][ty][wall + 1] = true;
                    }
                    else if (!day && (wall + 1 <= k) && !visited[tx][ty][wall + 1]) {
                        queue.offer(new Block(now.x, now.y, true, dist + 1, wall));
                    }
                }
                else {      // 벽 아님
                    if (!visited[tx][ty][wall]) {
                        queue.offer(new Block(tx, ty, !day, dist + 1, wall));
                        visited[tx][ty][wall] = true;
                    }
                }
            }
        }
        System.out.println(-1);
    }
    static class Block {
        public int x;
        public int y;
        public boolean day;
        public int dist;
        public int wall;
        public Block (int x, int y, boolean day, int dist, int wall) {
            this.x = x;
            this.y = y;
            this.day = day;
            this.dist = dist;
            this.wall = wall;
        }
    }
}
profile
Hello, I'm nunu

0개의 댓글