2026-rff_mp/ProninVV/task-2-oop/AStarStrategy.py

42 lines
1.1 KiB
Python
Raw Normal View History

2026-05-19 17:44:08 +00:00
from Maze import Cell, Maze
from strategy import PathFindingStrategy
class AStarStrategy(PathFindingStrategy):
def findPath(self, maze, start, exit):
2026-05-19 17:44:08 +00:00
def heuristik(cell):
return abs(cell.x - exit.x) + abs(cell.y - exit.y)
parents = {start: None}
queue = [start]
if not start or not exit:
return [], 0
2026-05-19 17:44:08 +00:00
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, len(parents)
2026-05-19 17:44:08 +00:00
childs = maze.getNeighbors(u)
for child in childs:
if child not in parents:
parents[child] = u
queue.append(child)
return [], len(parents)