Skip to content

Commit 9789240

Browse files
committed
getpath fixed
1 parent 47b1fc5 commit 9789240

1 file changed

Lines changed: 5 additions & 2 deletions

File tree

users/lib.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,15 @@ def getDistance(pos, pos2):
8282

8383
def 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

0 commit comments

Comments
 (0)