реализован поиск A*
This commit is contained in:
parent
2449eeb26f
commit
a27f861edf
41
ProninVV/task-2-oop/AStarStrategy.py
Normal file
41
ProninVV/task-2-oop/AStarStrategy.py
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
from Maze import Cell, Maze
|
||||
from strategy import PathFindingStrategy
|
||||
|
||||
|
||||
class AStarStrategy(PathFindingStrategy):
|
||||
def findPath(maze, start, exit):
|
||||
|
||||
def heuristik(cell):
|
||||
return abs(cell.x - exit.x) + abs(cell.y - exit.y)
|
||||
|
||||
if not start or not exit:
|
||||
return []
|
||||
|
||||
parents = {start: None}
|
||||
|
||||
queue = [start]
|
||||
|
||||
while len(queue) != 0:
|
||||
best_cell = queue[0]
|
||||
for cell in queue:
|
||||
if heuristik(cell) < heuristik(best_cell):
|
||||
best_cell = cell
|
||||
|
||||
u = best_cell
|
||||
queue.remove(u)
|
||||
|
||||
if u == exit:
|
||||
path = []
|
||||
current = exit
|
||||
while current is not None:
|
||||
path.append(current)
|
||||
current = parents[current]
|
||||
path.reverse()
|
||||
return path
|
||||
|
||||
childs = maze.getNeighbors(u)
|
||||
for child in childs:
|
||||
if child not in parents:
|
||||
parents[child] = u
|
||||
queue.append(child)
|
||||
return []
|
||||
Loading…
Reference in New Issue
Block a user