-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdijkstra.py
73 lines (61 loc) · 2.09 KB
/
dijkstra.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import pygame
from queue import PriorityQueue
def reconstruct(came, end, draw):
while end in came:
for event in pygame.event.get():
if event.type == pygame.QUIT:
return False
end = came[end]
end.make_path()
draw()
def h(p, q): # L distance
x1, y1 = p
x2, y2 = q
return abs(x1 - x2) + abs(y1 - y2)
def dijkstra(draw, grid, start, end, mode):
pygame.init()
count = 0
openset = PriorityQueue()
openset.put((0, count, start))
camefrom = {}
gscore = {node : float('inf') for row in grid for node in row}
gscore[start] = 0
fscore = {node : float('inf') for row in grid for node in row}
fscore[start] = h(start.get_pos(), end.get_pos())
opensethash = {start}
for row in grid:
for node in row:
node.update_neighbours(grid)
while not openset.empty():
for event in pygame.event.get():
if event.type == pygame.QUIT:
False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_BACKSPACE:
if mode == 0:
mode = 1
else:
mode = 0
current = openset.get()[2]
opensethash.remove(current)
if current == end:
reconstruct(camefrom, end, draw)
end.make_end()
start.make_start()
return True
for neighbour in current.neighbours:
tempgscore = gscore[current] + 1
if tempgscore < gscore[neighbour]:
camefrom[neighbour] = current
gscore[neighbour] = tempgscore
fscore[neighbour] = tempgscore + h(neighbour.get_pos(), end.get_pos())
if neighbour not in opensethash:
count += 1
openset.put((fscore, count, neighbour))
opensethash.add(neighbour)
neighbour.make_open()
if mode == 0:
draw()
if current != start:
current.make_closed()
return True