forked from UNN/2026-rff_mp
Реализован этап 3, добавлен A*, некоторые исправления
This commit is contained in:
parent
d5f28df86a
commit
107d5cbd61
|
|
@ -140,7 +140,7 @@
|
||||||
"name": "python",
|
"name": "python",
|
||||||
"nbconvert_exporter": "python",
|
"nbconvert_exporter": "python",
|
||||||
"pygments_lexer": "ipython3",
|
"pygments_lexer": "ipython3",
|
||||||
"version": "3.14.3"
|
"version": "3.14.5"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,45 @@
|
||||||
|
import heapq
|
||||||
|
from itertools import count
|
||||||
|
|
||||||
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 AStar(PathFindingStrategy):
|
class AStar(PathFindingStrategy):
|
||||||
"""Алгоритм с эвристикой (etc. манхэттенское расстояние) – компромисс между скоростью и оптимальностью."""
|
"""Алгоритм с эвристикой (etc. манхэттенское расстояние) – компромисс между скоростью и оптимальностью."""
|
||||||
|
def heuristic(self, first: Cell, second: Cell) -> int:
|
||||||
|
return abs(first.x - second.x) + abs(first.y - second.y)
|
||||||
|
|
||||||
def findPath(self, maze: Maze, start: Cell, exit: Cell):
|
def findPath(self, maze: Maze, start: Cell, exit: Cell):
|
||||||
...
|
tie_breaker = count()
|
||||||
|
start_heuristic = self.heuristic(start, exit)
|
||||||
|
heap: list[tuple[int, int, int, Cell]] = [
|
||||||
|
(start_heuristic, start_heuristic, next(tie_breaker), start)
|
||||||
|
]
|
||||||
|
g_score: dict[Cell, int] = {start: 0}
|
||||||
|
parents: dict[Cell, Cell | None] = {start: None}
|
||||||
|
visited: set[Cell] = set()
|
||||||
|
|
||||||
|
while heap:
|
||||||
|
_, _, _, current = heapq.heappop(heap)
|
||||||
|
if current in visited:
|
||||||
|
continue
|
||||||
|
visited.add(current)
|
||||||
|
|
||||||
|
if current == exit:
|
||||||
|
return restorePath(parents, exit)
|
||||||
|
|
||||||
|
for neighbor in maze.getNeighbors(current):
|
||||||
|
tentative_score = g_score[current]
|
||||||
|
if tentative_score < g_score.get(neighbor, 10**12):
|
||||||
|
g_score[neighbor] = tentative_score
|
||||||
|
parents[neighbor] = current
|
||||||
|
heuristic = self.heuristic(neighbor, exit)
|
||||||
|
priority = tentative_score + heuristic
|
||||||
|
heapq.heappush(
|
||||||
|
heap,
|
||||||
|
(priority, heuristic, next(tie_breaker), neighbor),
|
||||||
|
)
|
||||||
|
return []
|
||||||
|
|
@ -31,4 +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 restorePath(parents, start, exit)
|
return restorePath(parents, exit)
|
||||||
|
|
@ -31,4 +31,4 @@ class DFS(PathFindingStrategy):
|
||||||
parents[hood] = current
|
parents[hood] = current
|
||||||
stack.append(hood)
|
stack.append(hood)
|
||||||
|
|
||||||
return restorePath(parents, start, exit)
|
return restorePath(parents, exit)
|
||||||
|
|
@ -9,4 +9,5 @@ class PathFindingStrategy(ABC):
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def findPath(self, maze: Maze, start: Cell, exit: Cell):
|
def findPath(self, maze: Maze, start: Cell, exit: Cell):
|
||||||
"""Возвращает список клеток пути от старта до выхода включительно. Пути нет - пустой список."""
|
"""Возвращает список клеток пути от старта до выхода включительно. Пути нет - пустой список."""
|
||||||
|
raise NotImplementedError
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
from task2.mazeObjects.maze import Maze
|
from task2.mazeObjects.maze import Maze
|
||||||
from task2.mazeObjects.cell import Cell
|
from task2.mazeObjects.cell import Cell
|
||||||
|
|
||||||
def restorePath(parents: dict, start: Cell, exit: Cell) -> list[Cell]|None:
|
def restorePath(parents: dict, exit: Cell) -> list[Cell]|None:
|
||||||
path = []
|
path = []
|
||||||
current = exit
|
current = exit
|
||||||
while current:
|
while current:
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user