Skip to content

Commit 591db46

Browse files
authored
[20250908] PGM / LV2 / 게임 맵 최단거리 / 김수연
[20250908] PGM / LV2 / 게임 맵 최단거리 / 김수연
2 parents a3ab781 + 4065b1f commit 591db46

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
```java
2+
import java.util.*;
3+
class Solution {
4+
public int solution(int[][] maps) {
5+
6+
return bfs(maps);
7+
}
8+
public int bfs(int[][] maps) {
9+
int n = maps.length;
10+
int m = maps[0].length;
11+
int[][] d = new int[][]{{1,0}, {-1,0}, {0,1}, {0,-1}};
12+
int[][] visited = new int[n][m];
13+
Queue<int[]> q = new LinkedList<>();
14+
q.add(new int[]{0, 0, 1});
15+
visited[0][0] = 1;
16+
while (!q.isEmpty()) {
17+
int[] curr = q.poll();
18+
for (int[] move : d) {
19+
int ny = curr[0] + move[0];
20+
int nx = curr[1] + move[1];
21+
if (0 > ny || ny >= n || 0 > nx || nx >= m || maps[ny][nx] == 0 || visited[ny][nx] != 0) {
22+
continue;
23+
}
24+
visited[ny][nx] = curr[2] + 1;
25+
q.add(new int[]{ny, nx, curr[2]+1});
26+
if (ny == n-1 && nx == m-1) {
27+
return visited[ny][nx];
28+
}
29+
}
30+
}
31+
return -1;
32+
}
33+
}
34+
```

0 commit comments

Comments
 (0)