-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbfs.py
More file actions
37 lines (29 loc) · 1.03 KB
/
Copy pathbfs.py
File metadata and controls
37 lines (29 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
from collections import deque
# 상, 하, 좌, 우 탐색을 위한 방향 벡터
dx = [-1, 1, 0, 0]
dy = [0, 0, -1, 1]
def bfs(x, y, visited, grid):
queue = deque([(x, y)])
# 현재 위치를 방문했다고 표시
visited[x][y] = True
while queue:
x, y = queue.popleft()
print(f"방문 위치: ({x}, {y})")
# 현재 위치에서 인접한 위치 탐색
for d in range(4):
nx, ny = x + dx[d], y + dy[d]
# 배열 범위 안에 있고 방문하지 않았다면 큐에 넣고 탐색
if 0 <= nx < len(grid) and 0 <= ny < len(grid[0]) and not visited[nx][ny] and grid[nx][ny] == 1:
queue.append((nx, ny))
visited[nx][ny] = True
# 2차원 배열 예제 (1은 갈 수 있는 경로, 0은 갈 수 없는 경로)
grid = [
[1, 1, 1, 1],
[1, 0, 1, 1],
[0, 1, 1, 0],
[0, 1, 1, 1],
]
# 방문 정보
visited = [[False] * len(grid[0]) for _ in range(len(grid))]
# 예시로 (0, 0)부터 탐색 시작
bfs(0, 0, visited, grid)