[BOJ] 17836 공주님을 구해라

알파·2022년 7월 6일
0

Algorithm

목록 보기
20/20

bfs는 너비우선탐색하기 때문에 다 돌고 나면 알아서 최소값이 나옵니다..
핵심은 칼을 줍고 난 후에 방문했던 곳도 다시 갈 수 있도록 구현해야한다는 것.
그래서 거리와 칼 여부를 큐에 함께 담아줘야 한다.
visited가 여러 개 필요할 경우 3차원으로 정의해줘야한다.

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 Solution17836 {

    static int N, M, T, ans;
    static int[][] arr;
    static boolean[][][] visited;
    static int[] dx = {0, 0, -1, 1};
    static int[] dy = {1, -1, 0, 0};

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        N = Integer.parseInt(st.nextToken());
        M = Integer.parseInt(st.nextToken());
        T = Integer.parseInt(st.nextToken());
        arr = new int[N][M];
        visited = new boolean[2][N][M];
        for (int i = 0; i < N; i++) {
            st = new StringTokenizer(br.readLine());
            for (int j = 0; j < M; j++) {
                arr[i][j] = Integer.parseInt(st.nextToken());
            }
        }

        bfs(0, 0);

        if (ans == 0 || ans > T) {
            System.out.println("Fail");
        } else {
            System.out.println(ans);
        }
    }

    static void bfs(int x, int y) {
        visited[0][0][0] = true;
        Queue<int[]> q = new LinkedList<>();
        q.add(new int[]{x, y, 0, 0});
        while (!q.isEmpty()) {
            int[] xy = q.poll();
            x = xy[0];
            y = xy[1];
            int d = xy[2];
            int sword = xy[3];
            for (int i = 0; i < 4; i++) {
                int nx = x + dx[i];
                int ny = y + dy[i];
                if (nx >= 0 && nx < N && ny >= 0 && ny < M) {
                    if (sword == 0 && arr[nx][ny] == 0 && !visited[0][nx][ny]) {
                        visited[0][nx][ny] = true;
                        q.add(new int[]{nx, ny, d+1, 0});
                    } else if (sword == 1 && !visited[1][nx][ny]) {
                        visited[1][nx][ny] = true;
                        q.add(new int[]{nx, ny, d+1, 1});
                    } else if (sword == 0 && arr[nx][ny] == 2 && !visited[0][nx][ny]) {
                        visited[1][nx][ny] = true;
                        q.add(new int[]{nx, ny, d+1, 1});
                    }
                    if (nx == N - 1 && ny == M - 1) {
                        ans = d+1;
                        return;
                    }
                }
            }
        }
    }
}
profile
I am what I repeatedly do

0개의 댓글