-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbest_first_search.py
44 lines (37 loc) · 901 Bytes
/
best_first_search.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
40
41
42
43
44
import pygame
from queue import PriorityQueue
def color_best(draw, grid, path):
for node in path:
grid[node[0]][node[1]].make_path()
draw()
def h_best(node, end):
x1 = node.row
y1 = node.col
x2 = end.row
y2 = end.col
return abs(x1 - x2) + abs(y1 - y2)
def best_first_search(draw, grid, start, end):
path = []
pqueue = PriorityQueue()
t = (start.row, start.col)
q = (h_best(start,end), t)
pqueue.put(q)
while pqueue:
#print(pqueue)
u = pqueue.get()
#print(u)
t = u[1]
grid[t[0]][t[1]].make_closed()
path.append(t)
if grid[t[0]][t[1]] == end:
color_best(draw, grid, path)
return
else:
for neighbor in grid[t[0]][t[1]].neighbors:
if neighbor.isVisited[0] == 0:
neighbor.isVisited[0] = 1
t = (neighbor.row,neighbor.col)
q = (h_best(neighbor, end), t)
pqueue.put(q)
grid[t[0]][t[1]].make_open()
grid[t[0]][t[1]].isVisited[0] = 1