forked from UNN/2026-rff_mp
реализован алоритм Дейкстра
This commit is contained in:
parent
f89d10b615
commit
8331065da2
43
ProninVV/task-2-oop/Deikstra.py
Normal file
43
ProninVV/task-2-oop/Deikstra.py
Normal file
|
|
@ -0,0 +1,43 @@
|
||||||
|
from strategy import PathFindingStrategy
|
||||||
|
from Maze import Maze, Cell
|
||||||
|
|
||||||
|
|
||||||
|
class DeikstraFind(PathFindingStrategy):
|
||||||
|
def findPath(maze, start, exit):
|
||||||
|
|
||||||
|
if not start or not exit:
|
||||||
|
return []
|
||||||
|
|
||||||
|
queue = [start]
|
||||||
|
|
||||||
|
distances = {start: 0}
|
||||||
|
parents = {start: None}
|
||||||
|
|
||||||
|
while len(queue) != 0:
|
||||||
|
best_cell = queue[0]
|
||||||
|
for cell in queue:
|
||||||
|
if distances[cell] < distances[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)
|
||||||
|
|
||||||
|
for child in maze.getNeighbors(u):
|
||||||
|
distance_through_u = distances[u] + 1
|
||||||
|
|
||||||
|
if distance_through_u < distances.get(child, float('inf')):
|
||||||
|
distances[child] = distance_through_u
|
||||||
|
parents[child] = u
|
||||||
|
if child not in queue:
|
||||||
|
queue.append(child)
|
||||||
|
|
||||||
|
return []
|
||||||
42
ProninVV/task-2-oop/MazeSolver.py
Normal file
42
ProninVV/task-2-oop/MazeSolver.py
Normal file
|
|
@ -0,0 +1,42 @@
|
||||||
|
import time
|
||||||
|
|
||||||
|
|
||||||
|
class SearchStats:
|
||||||
|
def __init__(self, execution_time, visited_count, path_length, path):
|
||||||
|
self.execution_time = execution_time
|
||||||
|
self.visited_count = visited_count
|
||||||
|
self.path_length = path_length
|
||||||
|
self.path = path
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return ("f == Статистика поиска == =\n"
|
||||||
|
f"Время выполнения: {self.execution_time_ms:.4f} мс\n"
|
||||||
|
f"Посещено клеток: {self.visited_count}\n"
|
||||||
|
f"Длина пути: {self.path_length} клеток\n")
|
||||||
|
|
||||||
|
|
||||||
|
class MazeSolver:
|
||||||
|
def __init__(self, maze, strategy):
|
||||||
|
self._maze = maze
|
||||||
|
self._strategy = strategy
|
||||||
|
|
||||||
|
def setStrategy(self, strategy):
|
||||||
|
self._strategy = strategy
|
||||||
|
|
||||||
|
def solve(self):
|
||||||
|
|
||||||
|
if not self.maze or not self.strategy:
|
||||||
|
raise ValueError("Не задан лабиринт или стратегия поиска!")
|
||||||
|
|
||||||
|
start_time = time.perf_counter()
|
||||||
|
|
||||||
|
path, visited_count = self.strategy.findPath(
|
||||||
|
self.maze, self.maze.start, self.maze.exit)
|
||||||
|
|
||||||
|
end_time = time.perf_counter()
|
||||||
|
|
||||||
|
execution_time_ms = (end_time - start_time) * 1000
|
||||||
|
|
||||||
|
path_length = len(path)
|
||||||
|
|
||||||
|
return SearchStats(execution_time_ms, visited_count, path_length, path)
|
||||||
Loading…
Reference in New Issue
Block a user