File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -82,14 +82,15 @@ def getDistance(pos, pos2):
8282
8383def getPath (start , end ):
8484 openList = [start ]
85+ distance = {str (start ) : 0 }
8586 closedList = []
8687 parents = {}
8788
8889 while len (openList ) != 0 :
8990
9091 current = openList [0 ]
9192 for tmp in openList :
92- if getDistance (tmp , end ) < getDistance (current , end ):
93+ if distance [ str ( tmp )] + getDistance (tmp , end ) < distance [ str ( current )] + getDistance (current , end ):
9394 current = tmp
9495
9596 if current == end :
@@ -106,9 +107,11 @@ def getPath(start, end):
106107 elif not [X ,Y ] in openList :
107108 openList .append ([X ,Y ])
108109 parents [str ([X ,Y ])] = current
110+ distance [str ([X ,Y ])] = distance [str (current )] + 1
109111 else :
110- if not str ([X ,Y ]) in parents :
112+ if not str ([X ,Y ]) in parents or distance [ str ([ X , Y ])] > distance [ str ( current )] + 1 :
111113 parents [str ([X ,Y ])] = current
114+ distance [str ([X ,Y ])] = distance [str (current )] + 1
112115
113116 if current != end : # if the path does not exist
114117 return - 1
You can’t perform that action at this time.
0 commit comments