문제 설명
https://www.acmicpc.net/problem/2178
풀이과정
최단거리이므로 BFS를 이용해서 풀이한다.
정답코드
import java.io.*;
import java.util.*;
public class Main {
static int[] dy = {-1, 1, 0, 0};
static int[] dx = {0, 0, -1, 1};
static int[][] board;
static boolean[][] visited;
static int N, M;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
board = new int[N][M];
visited = new boolean[N][M];
for (int i = 0; i < N; i++) {
String input = br.readLine();
for (int j = 0; j < M; j++) {
board[i][j] = input.charAt(j) - '0';
}
}
bfs(0, 0);
bw.write(String.valueOf(board[N - 1][M - 1]));
bw.flush();
}
public static void bfs(int startY, int startX) {
Queue<int[]> queue = new LinkedList<>();
queue.add(new int[] {startY, startX});
visited[startY][startX] = true;
while (!queue.isEmpty()) {
int[] current = queue.poll();
int y = current[0];
int x = current[1];
for (int i = 0; i < 4; i++) {
int ny = y + dy[i];
int nx = x + dx[i];
if (ny >= 0 && ny < N && nx >= 0 && nx < M) {
if (!visited[ny][nx] && board[ny][nx] == 1) {
board[ny][nx] = board[y][x] + 1;
visited[ny][nx] = true;
queue.add(new int[] {ny, nx});
}
}
}
}
}
}