[Algorithm - Baekjoon] 14442번 벽 부수고 이동하기 2

nunu·2023년 9월 6일
0

Algorithm

목록 보기
71/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 - 입력

6 4 1
0100
1110
1000
0000
0111
0000

예제1 - 출력

15

예제2 - 입력

6 4 2
0100
1110
1000
0000
0111
0000

예제2 - 출력

9

예제2 - 입력

4 4 3
0111
1111
1111
1110

예제2 - 출력

-1

제출 코드

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());

        int[][] map = new int[n + 1][m + 1];
        for (int i = 1; i <= n; i++) {
            char[] input = br.readLine().toCharArray();
            for (int j = 1; j <= m; j++) {
                map[i][j] = input[j - 1] - '0';
            }
        }
        br.close();

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

        int[] mx = {0, 0, -1, 1};
        int[] my = {1, -1, 0, 0};
        visited[now.x][now.y][now.wall] = true;

        Queue<Block> queue = new LinkedList<>();
        queue.offer(now);
        while (!queue.isEmpty()) {
            now = queue.poll();
            if (now.x == n && now.y == m) {
                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)) {
                    int wall = now.wall;
                    int dist = now.dist;
                    if (map[tx][ty] == 0) {
                        if (!visited[tx][ty][wall]) {
                            visited[tx][ty][wall] = true;
                            queue.offer(new Block(tx, ty, dist + 1, wall));
                        }
                    }
                    else {
                        if ((wall + 1 <= k) && !visited[tx][ty][wall + 1]) {
                            visited[tx][ty][wall + 1] = true;
                            queue.offer(new Block(tx,ty, dist + 1, wall + 1));
                        }
                    }
                }
            }
        }
        System.out.println(-1);
    }
    static class Block {
        public int x;
        public int y;
        public int dist;
        public int wall;
        public Block(int x, int y, int d, int w) {
            this.x = x;
            this.y = y;
            dist = d;
            wall = w;
        }
    }
}
profile
Hello, I'm nunu

0개의 댓글