2026-rff_mp/stepushovgs/labyrinth/source/strategy/DFS.py

47 lines
1.6 KiB
Python

from source.strategy.strategy import PathFindingStrategy, reconstruct_path
from source.classes.maze import Maze
from source.classes.cell import Cell
class DFS(PathFindingStrategy):
@property
def name(self) -> str:
return "DFS"
def findPath(self, maze: Maze) -> tuple[list[Cell], int]:
start_cell = maze.start
exit_cell = maze.exit
print(f"Старт: {start_cell.getXY()}")
print(f"Выход: {exit_cell.getXY()}")
print(f"Соседи старта: {[n.getXY() for n in maze.getNeighbors(start_cell)]}")
stack = [start_cell]
parents = {start_cell.getXY(): Cell(-1, -1)}
visited = {start_cell.getXY()}
count_visited = 0
while stack:
current = stack.pop()
if current.getXY() == exit_cell.getXY():
return reconstruct_path(
came_from=parents,
start=start_cell,
end=current
), count_visited
neigbours = maze.getNeighbors(current)
print(f"для клекти {current.getXY()} соседи: {[neigbour.getXY() for neigbour in neigbours]}")
for neighbor in maze.getNeighbors(current):
neig_xy = neighbor.getXY()
if neig_xy not in visited:
visited.add(neig_xy)
parents[neig_xy] = current
count_visited += 1
# new_path = current_path + [neigbour]
stack.append(neighbor)
return [], count_visited