From 8331065da2872f47dc53f6b4305e634413655e40 Mon Sep 17 00:00:00 2001 From: Proninvv Date: Tue, 19 May 2026 22:06:13 +0300 Subject: [PATCH] =?UTF-8?q?=D1=80=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=BE?= =?UTF-8?q?=D0=B2=D0=B0=D0=BD=20=D0=B0=D0=BB=D0=BE=D1=80=D0=B8=D1=82=D0=BC?= =?UTF-8?q?=20=D0=94=D0=B5=D0=B9=D0=BA=D1=81=D1=82=D1=80=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ProninVV/task-2-oop/Deikstra.py | 43 +++++++++++++++++++++++++++++++ ProninVV/task-2-oop/MazeSolver.py | 42 ++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 ProninVV/task-2-oop/Deikstra.py create mode 100644 ProninVV/task-2-oop/MazeSolver.py 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)