Skip to content

Commit 63b3581

Browse files
committed
[23.01.04/Python] 미로 탐색 (BFS)
1 parent 594c79a commit 63b3581

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

Python_BOJ_2023/2178.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# 미로 탐색
2+
from collections import deque
3+
dx, dy = [-1, 1, 0, 0], [0, 0, -1, 1]
4+
5+
n, m = map(int, input().split())
6+
graph = [list(map(int, input())) for _ in range(n)]
7+
visited = [[0] * m for _ in range(n)]
8+
9+
def bfs():
10+
queue = deque()
11+
queue.append((0, 0))
12+
visited[0][0] = 1
13+
14+
while queue:
15+
x, y = queue.popleft()
16+
for i in range(4):
17+
nx, ny = x + dx[i], y + dy[i]
18+
if 0 <= nx < n and 0 <= ny < m:
19+
if not visited[nx][ny] and graph[nx][ny] == 1:
20+
visited[nx][ny] = visited[x][y] + 1
21+
queue.append((nx, ny))
22+
23+
bfs()
24+
print(visited[n - 1][m - 1])

0 commit comments

Comments
 (0)