diff --git "a/suyeun84/202509/08 PGM LV2 \352\262\214\354\236\204 \353\247\265 \354\265\234\353\213\250\352\261\260\353\246\254.md" "b/suyeun84/202509/08 PGM LV2 \352\262\214\354\236\204 \353\247\265 \354\265\234\353\213\250\352\261\260\353\246\254.md" new file mode 100644 index 00000000..39d39de7 --- /dev/null +++ "b/suyeun84/202509/08 PGM LV2 \352\262\214\354\236\204 \353\247\265 \354\265\234\353\213\250\352\261\260\353\246\254.md" @@ -0,0 +1,34 @@ +```java +import java.util.*; +class Solution { + public int solution(int[][] maps) { + + return bfs(maps); + } + public int bfs(int[][] maps) { + int n = maps.length; + int m = maps[0].length; + int[][] d = new int[][]{{1,0}, {-1,0}, {0,1}, {0,-1}}; + int[][] visited = new int[n][m]; + Queue q = new LinkedList<>(); + q.add(new int[]{0, 0, 1}); + visited[0][0] = 1; + while (!q.isEmpty()) { + int[] curr = q.poll(); + for (int[] move : d) { + int ny = curr[0] + move[0]; + int nx = curr[1] + move[1]; + if (0 > ny || ny >= n || 0 > nx || nx >= m || maps[ny][nx] == 0 || visited[ny][nx] != 0) { + continue; + } + visited[ny][nx] = curr[2] + 1; + q.add(new int[]{ny, nx, curr[2]+1}); + if (ny == n-1 && nx == m-1) { + return visited[ny][nx]; + } + } + } + return -1; + } +} +```