-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdistances.py
42 lines (33 loc) · 1.11 KB
/
distances.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
class Distances:
def __init__(self, root):
self.root = root
self.cells = {}
self.cells[root] = 0
def get_cells(self):
return self.cells.keys()
def get(self, cell):
if cell in self.cells:
return self.cells[cell]
else:
return None
def set(self, cell, distance):
self.cells[cell] = distance
def path_to(self, goal):
current = goal
breadcrumbs = Distances(self.root)
breadcrumbs.cells[current] = self.cells[current]
while not current == self.root:
for neighbor in current.get_links():
if self.cells[neighbor] < self.cells[current]:
breadcrumbs.cells[neighbor] = self.cells[neighbor]
current = neighbor
break
return breadcrumbs
def max(self):
max_distance = 0
max_cell = self.root
for cell in self.cells:
if self.cells[cell] > max_distance:
max_distance = self.cells[cell]
max_cell = cell
return [max_cell, max_distance]