→ bfs

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;

public class Main {
    static int N, M;
    static int[][] maze;
    static boolean[][] visited;
    static int[] dx = {-1, 1, 0, 0}; // 상하좌우
    static int[] dy = {0, 0, -1, 1};

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] input = br.readLine().split(" ");
        N = Integer.parseInt(input[0]);
        M = Integer.parseInt(input[1]);

        maze = new int[N][M];
        visited = new boolean[N][M];

        for (int i = 0; i < N; i++) {
            String row = br.readLine();
            for (int j = 0; j < M; j++) {
                maze[i][j] = row.charAt(j) - '0';
            }
        }

        bfs(0, 0); // 출발지 좌표 (0, 0)에서 BFS 수행

        System.out.println(maze[N - 1][M - 1]); // 도착지의 최소 이동 횟수 출력
    }

    public static void bfs(int x, int y) {
        Queue<int[]> queue = new LinkedList<>();
        queue.offer(new int[]{x, y}); // 출발지 좌표를 큐에 추가
        visited[x][y] = true; // 출발지 방문 처리

        while (!queue.isEmpty()) {
            int[] current = queue.poll();
            int cx = current[0];
            int cy = current[1];

            for (int i = 0; i < 4; i++) {
                int nx = cx + dx[i];
                int ny = cy + dy[i];

                if (nx >= 0 && nx < N && ny >= 0 && ny < M) {
                    if (maze[nx][ny] == 1 && !visited[nx][ny]) {
                        queue.offer(new int[]{nx, ny});
                        visited[nx][ny] = true;
                        maze[nx][ny] = maze[cx][cy] + 1; // 이동 횟수 갱신
                    }
                }
            }
        }
    }
}

<aside> ☝ stringTokenizer보다 split() 쓰는 이유 ?

</aside>

 public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] input = br.readLine().split(" ");
        N = Integer.parseInt(input[0]);
        M = Integer.parseInt(input[1]);

**StringTokenizer**는 JDK 1.0부터 제공되었으며, 문자열을 구분자(delimiter)를 기준으로 나누는 기능을 제공합니다. 그러나 **StringTokenizer**는 더 이상 권장되지 않는 클래스입니다.

반면, split() 메서드는 String 클래스의 메서드로 JDK 1.4부터 도입되었으며, 정규식을 사용하여 문자열을 분할할 수 있습니다. split() 메서드를 사용하면 더 간단하고 직관적인 코드를 작성할 수 있습니다.

또한, 성능 면에서도 split() 메서드가 **StringTokenizer**보다 효율적입니다.

split() 메서드는 내부적으로 정규식을 사용하고, JVM은 정규식 엔진을 최적화하여 성능을 향상시켰습니다. 반면, **StringTokenizer**는 단순히 구분자를 기준으로 문자열을 나누기 때문에

split() 메서드보다는 느릴 수 있습니다.

split() 메서드를 사용하는 것이 보다 간편하고 성능적으로 더 우수합니다.

**StringTokenizer**보다는 split() 메서드를 사용하는 것이 권장됩니다.

<aside> ☝ 보통 bfs는 queue를 쓰고, dfs는 ArrayList를 쓰나?

</aside>