diff --git a/ProninVV/task-2-oop/Deikstra.py b/ProninVV/task-2-oop/Deikstra.py new file mode 100644 index 0000000..8c2524d --- /dev/null +++ b/ProninVV/task-2-oop/Deikstra.py @@ -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 [] diff --git a/ProninVV/task-2-oop/MazeSolver.py b/ProninVV/task-2-oop/MazeSolver.py new file mode 100644 index 0000000..cca3279 --- /dev/null +++ b/ProninVV/task-2-oop/MazeSolver.py @@ -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)