forked from UNN/2026-rff_mp
Compare commits
2 Commits
89c11085ba
...
d5f28df86a
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d5f28df86a | ||
|
|
1a562a7594 |
|
|
@ -1,4 +1,5 @@
|
||||||
from task2.strategyObjects.pathFindingStrategy import PathFindingStrategy
|
from task2.strategyObjects.pathFindingStrategy import PathFindingStrategy
|
||||||
|
from task2.strategyObjects.util import restorePath
|
||||||
|
|
||||||
from task2.mazeObjects.maze import Maze
|
from task2.mazeObjects.maze import Maze
|
||||||
from task2.mazeObjects.cell import Cell
|
from task2.mazeObjects.cell import Cell
|
||||||
|
|
@ -6,7 +7,8 @@ from task2.mazeObjects.cell import Cell
|
||||||
import queue
|
import queue
|
||||||
|
|
||||||
class BFS(PathFindingStrategy):
|
class BFS(PathFindingStrategy):
|
||||||
"""Поиск в ширину – гарантирует кратчайший путь по количеству шагов."""
|
"""Поиск в ширину – гарантирует кратчайший путь по количеству шагов.
|
||||||
|
Возвращает None, если пути нет"""
|
||||||
def findPath(self, maze: Maze, start: Cell, exit: Cell):
|
def findPath(self, maze: Maze, start: Cell, exit: Cell):
|
||||||
visited = dict()
|
visited = dict()
|
||||||
parents = dict()
|
parents = dict()
|
||||||
|
|
@ -29,14 +31,4 @@ class BFS(PathFindingStrategy):
|
||||||
visited[hood] = visited[current] + 1
|
visited[hood] = visited[current] + 1
|
||||||
parents[hood] = current
|
parents[hood] = current
|
||||||
q.put(hood)
|
q.put(hood)
|
||||||
return self.restorePath(parents, start, exit)
|
return restorePath(parents, start, exit)
|
||||||
|
|
||||||
def restorePath(self, parents: dict, start: Cell, exit: Cell) -> list[Cell]|None:
|
|
||||||
path = []
|
|
||||||
current = exit
|
|
||||||
while current:
|
|
||||||
path.append(current)
|
|
||||||
if current not in parents:
|
|
||||||
return []
|
|
||||||
current = parents[current]
|
|
||||||
return path[::-1]
|
|
||||||
|
|
@ -1,9 +1,34 @@
|
||||||
from task2.strategyObjects.pathFindingStrategy import PathFindingStrategy
|
from task2.strategyObjects.pathFindingStrategy import PathFindingStrategy
|
||||||
|
from task2.strategyObjects.util import restorePath
|
||||||
|
|
||||||
from task2.mazeObjects.maze import Maze
|
from task2.mazeObjects.maze import Maze
|
||||||
from task2.mazeObjects.cell import Cell
|
from task2.mazeObjects.cell import Cell
|
||||||
|
|
||||||
|
|
||||||
class DFS(PathFindingStrategy):
|
class DFS(PathFindingStrategy):
|
||||||
"""Поиск в глубину – быстрый, но не обязательно кратчайший."""
|
"""Поиск в глубину – быстрый, но не обязательно кратчайший.
|
||||||
|
Возвращает None, если пути нет"""
|
||||||
def findPath(self, maze: Maze, start: Cell, exit: Cell):
|
def findPath(self, maze: Maze, start: Cell, exit: Cell):
|
||||||
...
|
visited = dict()
|
||||||
|
parents = dict()
|
||||||
|
stack = []
|
||||||
|
|
||||||
|
stack.append(start)
|
||||||
|
visited[start] = 0
|
||||||
|
parents[start] = None
|
||||||
|
|
||||||
|
while stack:
|
||||||
|
current = stack.pop()
|
||||||
|
|
||||||
|
# Условие нахождение выхода
|
||||||
|
if current == exit: break
|
||||||
|
|
||||||
|
# Перебор соседей
|
||||||
|
for hood in maze.getNeighbors(current):
|
||||||
|
if hood in visited:
|
||||||
|
continue
|
||||||
|
visited[hood] = visited[current] + 1
|
||||||
|
parents[hood] = current
|
||||||
|
stack.append(hood)
|
||||||
|
|
||||||
|
return restorePath(parents, start, exit)
|
||||||
12
MusinAA/task2/strategyObjects/util.py
Normal file
12
MusinAA/task2/strategyObjects/util.py
Normal file
|
|
@ -0,0 +1,12 @@
|
||||||
|
from task2.mazeObjects.maze import Maze
|
||||||
|
from task2.mazeObjects.cell import Cell
|
||||||
|
|
||||||
|
def restorePath(parents: dict, start: Cell, exit: Cell) -> list[Cell]|None:
|
||||||
|
path = []
|
||||||
|
current = exit
|
||||||
|
while current:
|
||||||
|
path.append(current)
|
||||||
|
if current not in parents:
|
||||||
|
return None
|
||||||
|
current = parents[current]
|
||||||
|
return path[::-1]
|
||||||
Loading…
Reference in New Issue
Block a user