-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbfs.py
39 lines (27 loc) · 800 Bytes
/
bfs.py
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
38
39
import pygame
def color_bfs_path(path, grid, draw):
for node in path:
grid[node[0]][node[1]].make_path()
draw()
def bfs(draw, grid, start, end):
queue = []
t = (start.row, start.col)
queue.append([t])
while queue:
path = []
path = queue.pop(0)
node = path[-1]
grid[node[0]][node[1]].isVisited[0] = 1;
grid[node[0]][node[1]].make_closed()
if grid[node[0]][node[1]] == end:
color_bfs_path(path, grid, draw)
return
for adjacent in grid[node[0]][node[1]].neighbors:
if (grid[adjacent.row][adjacent.col]).isVisited[0] == 0:
adjacent.make_open()
new_path = list(path)
t = (adjacent.row, adjacent.col)
new_path.append(t)
queue.append(new_path)
grid[adjacent.row][adjacent.col].isVisited[0] = 1
draw()