We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 884e237 commit 91b2506Copy full SHA for 91b2506
1 file changed
minjeong/DFSBFS/2025-05-02-[백준]-#2178-미로탐색.py
@@ -0,0 +1,39 @@
1
+import sys
2
+from collections import deque
3
+input = sys.stdin.readline
4
+
5
+# 입력 처리
6
+N, M = map(int, input().split())
7
+graph = [list(map(int, input().strip())) for _ in range(N)]
8
9
+def bfs(x, y):
10
+ # 방향: 상, 하, 좌, 우
11
+ dx = [-1, 1, 0, 0]
12
+ dy = [0, 0, -1, 1]
13
14
+ queue = deque()
15
+ queue.append((x, y))
16
17
+ while queue:
18
+ x, y = queue.popleft()
19
20
+ for i in range(4):
21
+ nx = x + dx[i]
22
+ ny = y + dy[i]
23
24
+ # 범위 벗어나면 무시
25
+ if nx < 0 or nx >= N or ny < 0 or ny >= M:
26
+ continue
27
28
+ # 벽이면 무시
29
+ if graph[nx][ny] == 0:
30
31
32
+ # 이동 가능한 칸이면 거리 갱신 후 방문 처리
33
+ if graph[nx][ny] == 1:
34
+ graph[nx][ny] = graph[x][y] + 1
35
+ queue.append((nx, ny))
36
37
+ return graph[N-1][M-1]
38
39
+print(bfs(0, 0))
0 commit comments