본문 바로가기

알고리즘/초급1

[JAVA] 백준 2178번: 미로 탐색 ( 초급 2-41 )

문제

N×M크기의 배열로 표현되는 미로가 있다.

1 0 1 1 1 1
1 0 1 0 1 0
1 0 1 0 1 1
1 1 1 0 1 1

미로에서 1은 이동할 수 있는 칸을 나타내고, 0은 이동할 수 없는 칸을 나타낸다. 이러한 미로가 주어졌을 때, (1, 1)에서 출발하여 (N, M)의 위치로 이동할 때 지나야 하는 최소의 칸 수를 구하는 프로그램을 작성하시오. 한 칸에서 다른 칸으로 이동할 때, 서로 인접한 칸으로만 이동할 수 있다.

위의 예에서는 15칸을 지나야 (N, M)의 위치로 이동할 수 있다. 칸을 셀 때에는 시작 위치와 도착 위치도 포함한다.

입력

첫째 줄에 두 정수 N, M(2 ≤ N, M ≤ 100)이 주어진다. 다음 N개의 줄에는 M개의 정수로 미로가 주어진다. 각각의 수들은 붙어서 입력으로 주어진다.

출력

첫째 줄에 지나야 하는 최소의 칸 수를 출력한다. 항상 도착위치로 이동할 수 있는 경우만 입력으로 주어진다.

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 baekjoon2718 {
    private static int targetRow;
    private static int targetCol;
    private static int[] rowTemp = { -1, 1, 0, 0 };
    private static int[] colTemp = { 0, 0, -1, 1 };
    private static int[][] info;
    private static boolean[][] visit;

    public static void main(String[] args) throws IOException {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());

        targetRow = Integer.parseInt(st.nextToken());
        targetCol = Integer.parseInt(st.nextToken());

        // 미로 정보를 담을 공간 초기화
        info = new int[targetRow][targetCol];
        // 방문 여부 확인
        visit = new boolean[targetRow][targetCol];

        // 미로 정보 저장
        for (int i = 0; i < targetRow; i++) {
            String temp = br.readLine();
            for (int j = 0; j < targetCol; j++) {
                info[i][j] = Character.getNumericValue(temp.charAt(j));
            }
        }

        bfs2718();
    }

    private static void bfs2718() {
        Queue<Maze> queue = new LinkedList<>();
        queue.add(new Maze(0, 0, 1));
        visit[0][0] = true;

        int row;
        int col;
        int count;
        int nextRow;
        int nextCol;

        while (!queue.isEmpty()) {
            Maze maze = queue.remove();
            row = maze.height;
            col = maze.width;
            count = maze.count;

            if (row == targetRow - 1 && col == targetCol - 1) {
                System.out.println(count);
                System.exit(0);
            }

            for (int i = 0; i < 4; i++) {
                nextRow = row + rowTemp[i];
                nextCol = col + colTemp[i];
                if (nextRow >= 0 && nextCol >= 0 && nextRow < targetRow && nextCol < targetCol) {
                    if (info[nextRow][nextCol] != 0 && !visit[nextRow][nextCol]) {
                        visit[nextRow][nextCol] = true;
                        queue.add(new Maze(nextRow, nextCol, count + 1));
                    }
                }
            }
        }
    }
}

class Maze {
    int height;
    int width;
    int count;

    Maze(int height, int width, int count) {
        this.height = height;
        this.width = width;
        this.count = count;
    }
}