[백준] 14442번 : 벽 부수고 이동하기 2 - JAVA [자바]

가오리·2024년 1월 12일
0
post-thumbnail

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


bfs 알고리즘 문제이다.

[백준] 2206번 : 벽 부수고 이동하기 - JAVA [자바]를 풀어봤으면 쉽게 풀 수 있다.

위의 문제와 다른 점은 벽을 K개 까지 부술 수 있다는 것이다.

visited[N][M][K+1]로 3중 배열을 만들어서 어떠한 곳에 방문 했을 때 지금까지 부순 벽의 갯수도 생각해서 방문 여부를 처리한다.


public class bj14442 {

    public static Queue<player> queue = new LinkedList<>();
    public static int N, M, K, ANSWER = -1;
    public static boolean[][][] visited;
    public static int[][] map;
    static int[] xMove = {1, -1, 0, 0};
    static int[] yMove = {0, 0, -1, 1};

    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

        String line = br.readLine();
        String[] split = line.split(" ");
        N = Integer.parseInt(split[0]);
        M = Integer.parseInt(split[1]);
        K = Integer.parseInt(split[2]);

        map = new int[N][M];

        for (int i = 0; i < N; i++) {
            String line1 = br.readLine();
            char[] charArray = line1.toCharArray();
            for (int j = 0; j < M; j++) {
                map[i][j] = Integer.parseInt(String.valueOf(charArray[j]));
            }
        }

        visited = new boolean[N][M][K + 1];

        bfs(new player(0, 0, 1, 0));

        bw.write(ANSWER + "\n");
        bw.flush();
        bw.close();
        br.close();
    }

    public static void bfs(player start) {
        queue.add(start);
        visited[start.x][start.y][0] = true;

        while (!queue.isEmpty()) {
            player current = queue.poll();
            if (current.x == N - 1 && current.y == M - 1) {
                ANSWER = current.count;
                break;
            }
            for (int i = 0; i < 4; i++) {
                int newX = current.x + xMove[i];
                int newY = current.y + yMove[i];

                if (newX < 0 || newY < 0 || newX >= N || newY >= M) continue;

                // 벽이 아니면
                if (map[newX][newY] != 1) {
                    if (!visited[newX][newY][current.breakWallNum]) {
                        visited[newX][newY][current.breakWallNum] = true;
                        queue.add(new player(newX, newY, current.count + 1, current.breakWallNum));
                    }
                } // 벽이면
                else {
                    // 지금까지 부신 벽의 개수가 K 개를 안넘었을 때
                    if (current.breakWallNum < K) {
                        if (!visited[newX][newY][current.breakWallNum + 1]) {
                            visited[newX][newY][current.breakWallNum + 1] = true;
                            queue.add(new player(newX, newY, current.count + 1, current.breakWallNum + 1));
                        }
                    }
                }
            }
        }
    }

    public static class player {
        int x;
        int y;
        int count;
        int breakWallNum;

        public player(int x, int y, int count, int breakWallNum) {
            this.x = x;
            this.y = y;
            this.count = count;
            this.breakWallNum = breakWallNum;
        }
    }
}
profile
가오리의 개발 이야기

0개의 댓글