pkuImoogis / study-codingTest

0 stars 0 forks source link

2206 #177

Open pagh2322 opened 7 months ago

pagh2322 commented 7 months ago

문제 링크

pagh2322 commented 7 months ago

현재 위치(i, j)에 도착했을 때, 벽을 부순 후 도착하거나, 그냥 도착하는 경우가 있어요. 2가지 경우에 맞는 방문 처리 배열을 만들었어요.

만약 인접한 곳이 벽이라면,

  1. 현재 위치까지 도달하면서 벽을 안 부쉈고,
  2. 인접한 곳에 벽을 부수지 않았을 때 방문한 적이 없다면,

방문 처리를 한 뒤(visited[x][y][0] = true), 벽을 부쉈다고 표시해서 큐에 삽입해요.

만약 인접한 곳이 벽이 아니라면,

  1. 현재 위치까지 도달하면서 벽을 부쉈고,
  2. 인접한 곳에 벽을 부순 뒤, 도달한 적이 있으면,

방문 처리를 한 뒤(visited[x][y][1] = true), 벽을 부쉈다고 표시해서 큐에 삽입해요. 또는,

  1. 현재 위치까지 도달하면서 벽을 안 부쉈고,
  2. 인접한 곳에 벽을 부수지 않았을 때 방문한 적이 없다면,

방문 처리를 한 뒤(visited[x][y][0] = true), 벽을 안 부쉈다고 표시해서 큐에 삽입해요.

코드

public class Main {

    static int[][] graph;
    static int[] dx = {-1, 1, 0, 0}, dy = {0, 0, -1, 1};
    static int N, M;

    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());
        graph = new int[N][M];
        for (int i = 0; i < N; i++) {
            st = new StringTokenizer(br.readLine());
            String[] line = st.nextToken().split("");
            for (int j = 0; j < M; j++)
                graph[i][j] = line[j].charAt(0) - '0';
        }
        System.out.println(bfs());
    }

    static int bfs() {
        boolean[][][] visited = new boolean[N][M][2];
        Queue<Node> queue = new LinkedList<>();
        queue.offer(new Node(0, 0, 1, false));
        visited[0][0][0] = true;

        while (!queue.isEmpty()) {
            Node curr = queue.poll();

            if (curr.x == N - 1 && curr.y == M - 1)
                return curr.distance;

            for (int i = 0; i < 4; i++) {
                int nextX = curr.x + dx[i], nextY = curr.y + dy[i];

                if (nextX < 0 || nextX >= N || nextY < 0 || nextY >= M) continue;

                if (graph[nextX][nextY] == 0) {
                    if (curr.broken && !visited[nextX][nextY][1]) {
                        visited[nextX][nextY][1] = true;
                        queue.offer(new Node(nextX, nextY, curr.distance + 1, true));
                    }
                    else if (!curr.broken && !visited[nextX][nextY][0]) {
                        visited[nextX][nextY][0] = true;
                        queue.offer(new Node(nextX, nextY, curr.distance + 1, false));
                    }
                }
                else {
                    if (!curr.broken && !visited[nextX][nextY][0]) {
                        visited[nextX][nextY][0] = true;
                        queue.offer(new Node(nextX, nextY, curr.distance + 1, true));
                    }
                }
            }
        }
        return -1;
    }
}

class Node {
    int x, y, distance;
    boolean broken;

    public Node(int x, int y, int distance, boolean broken) {
        this.x = x;
        this.y = y;
        this.distance = distance;
        this.broken = broken;
    }
}