미로 + 최솟값이므로 BFS를 쓴다.
BFS는 기본적으로
package day5.B2178;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Array;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
public class Main {
static int N, M;
static int[][] map;
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());
map = new int[N][M];
visited = new boolean[N][M];
for (int i = 0; i < N; i++) {
String line = br.readLine();
for (int j = 0; j < M; j++) {
map[i][j] = line.charAt(j) - '0';
}
}
bfs(0, 0); // 시작 지점 (0,0)
System.out.println(map[N - 1][M - 1]); // 도착 지점의 거리 출력
}
public static void bfs(int y, int x) {
Queue<int[]> queue = new LinkedList<>();
queue.offer(new int[]{y, x});
visited[y][x] = true;
while (!queue.isEmpty()) {
int[] now = queue.poll();
int curY = now[0];
int curX = now[1];
for (int i = 0; i < 4; i++) {
int ny = curY + dy[i];
int nx = curX + dx[i];
// 범위 체크
if (ny < 0 || nx < 0 || ny >= N || nx >= M)
continue;
// 이동할 수 있는 길이고, 아직 방문 안 한 곳
if (map[ny][nx] == 1 && !visited[ny][nx]) {
visited[ny][nx] = true;
map[ny][nx] = map[curY][curX] + 1; // 거리 갱신
queue.offer(new int[]{ny, nx});
}
}
}
}
}
for (int j = 0; j < M; j++) { map[i][j] = line.charAt(j) - '0'; }
이 코드에서 왜 문자열로 받지? int로 받으면 안돼?
이 코드의 핵심 : static dx, dy를 두고 상하좌우를 확인한다. 이때, now[0], now[1]이 뭘 의미하는지 알아야 한다.
queue에 현재 int[] 를 넣고 있는데, 이는 (x,y) 좌표이다.
이걸 poll로 뺄 때 now[0], now[1]로 받는 것
즉 x = now[0], y = now[1];