Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions suyeun84/202509/08 PGM LV2 게임 맵 최단거리.md
Original file line number Diff line number Diff line change
@@ -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<int[]> 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;
}
}
```