-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpathfinding.py
60 lines (49 loc) · 1.54 KB
/
pathfinding.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
def sdfs(start, end):
road = list()
visited, queue = set(), [start]
while len(queue) != 0:
point = queue.pop()
if point not in visited and point.is_safe and point.is_permitted:
road.append(point)
visited.add(point)
if point == end:
return road
for n in point.neighbours.values():
queue.append(n)
return road
def dfs(start, end):
open_list = set([start])
closed_list = set()
g = {}
g[start] = 0
pairs = {}
pairs[start] = start
while len(open_list) > 0:
n = None
for v in open_list:
if n == None or g[v] < g[n]:
n = v
if n == None:
return None
if n == end:
reconst_path = []
while pairs[n] != n:
reconst_path.append(n)
n = pairs[n]
reconst_path.append(start)
reconst_path.reverse()
return reconst_path
for m in list(filter(lambda x: x.is_safe and x.is_permitted, n.neighbours.values())):
if m not in open_list and m not in closed_list:
open_list.add(m)
pairs[m] = n
g[m] = g[n] + 1
else:
if g[m] > g[n] + 1:
g[m] = g[n] + 1
pairs[m] = n
if m in closed_list:
closed_list.remove(m)
open_list.add(m)
open_list.remove(n)
closed_list.add(n)