int[] dx = { 1, 0, -1, 0 };
int[] dy = { 0, 1, 0, -1 };
Queue<int[]> queue = new LinkedList<>();
public int solution(int[][] maps) {
int answer = 0;
int[][] visited = new int[maps.length][maps[0].length];
visited[0][0] = 1;
queue.add(new int[] { 0, 0 });
bfs(maps, visited);
answer = visited[maps.length - 1][maps[0].length - 1];
if (answer == 0) {
answer = -1;
}
return answer;
}
public void bfs(int[][] maps, int[][] visited) {
while (!queue.isEmpty()) {
int[] current = queue.poll();
int currentX = current[0];
int currentY = current[1];
for (int i = 0; i < 4; i++) {
int nX = currentX + dx[i];
int nY = currentY + dy[i];
if (nX < 0 || nX > maps.length - 1 || nY < 0 || nY > maps[0].length - 1)
continue;
if (visited[nX][nY] == 0 && maps[nX][nY] == 1) {
visited[nX][nY] = visited[currentX][currentY] + 1;
queue.add(new int[] { nX, nY });
}
}
}
}
출처:https://school.programmers.co.kr/learn/courses/30/lessons/1844