static int[] dx = { 1, 0, -1, 0 };
static int[] dy = { 0, 1, 0, -1 };
static Queue<int[]> queue = new LinkedList<>();
public static int solution(int[][] maps) {
int answer = 0;
int[][] visited = new int[maps.length][maps[0].length];
int x = 0;
int y = 0;
visited[x][y] = 1;
queue.add(new int[] { x, y });
bfs(maps, visited);
answer = visited[maps.length - 1][maps[0].length - 1];
if (answer == 0) {
answer = -1;
}
return answer;
}
public static 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 });
}
}
}
}