From a830e4d7a2a395d8185de054e2aece6bb1fc9849 Mon Sep 17 00:00:00 2001 From: yanyaevaa Date: Sat, 9 May 2026 22:37:10 +0300 Subject: [PATCH] =?UTF-8?q?=D0=92=D1=8B=D0=BF=D0=BE=D0=BB=D0=BD=D0=B5?= =?UTF-8?q?=D0=BD=D1=8B=20=D1=8D=D1=82=D0=B0=D0=BF=D1=8B=201,=202,=20?= =?UTF-8?q?=D0=B2=203=20=D1=80=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=BE=D0=B2?= =?UTF-8?q?=D0=B0=D0=BD=D0=BE=20BFS?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- YanyaevAA/task2/task_2.py | 100 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 YanyaevAA/task2/task_2.py diff --git a/YanyaevAA/task2/task_2.py b/YanyaevAA/task2/task_2.py new file mode 100644 index 0000000..be266f6 --- /dev/null +++ b/YanyaevAA/task2/task_2.py @@ -0,0 +1,100 @@ +from abc import ABC, abstractmethod +from collections import deque +#Этап 1 +class Cell: + def __init__(self, x, y, is_wall=False, is_start=False, is_exit=False): + self.x = x + self.y = y + self.is_wall = is_wall + self.is_start = is_start + self.is_exit = is_exit + + def isPassable(self): + return not self.is_wall + +class Maze: + def __init__(self, cells, width, height, start, exit): + self.width = width + self.height = height + self.cells =cells + self.start = start + self.exit = exit + + def getCell(self, x, y): + if 0 <= x< self.width and 0 <=y< self.height: + return self.cells[y][x] + return None + + def getNeighbors(self, cell: Cell): + neighbors = [] + directions = [(0, -1), (0, 1), (-1, 0), (1, 0)] + + for dir_x, dir_y in directions: + neigh_x = cell.x+dir_x + neigh_y = cell.y+dir_y + neighbor = self.getCell(neigh_x, neigh_y) + if neighbor and neighbor.isPassable(): + neighbors.append(neighbor) + return neighbors + +#Этап 2 +class MazeBuilder(ABC): + @abstractmethod + def buildFromFile(self, filename): + pass + +class TextFileMazeBuilder(MazeBuilder): + def buildFromFile(self, filename): + with open(filename, 'r') as f: + lines = [line.rstrip('\n') for line in f] + height = len(lines) + width = max(len(line) for line in lines) + + grid=[] + start_cell=None + exit_cell=None + for y in range(height): + row=[] + for x in range(width): + char=lines[y][x] + + is_wall = (char == '#') + is_start = (char == 'S') + is_exit = (char == 'E') + + cell=Cell(x, y, is_wall, is_start, is_exit) + + if is_start: + start_cell =cell + if is_exit: + exit_cell =cell + row.append(cell) + grid.append(row) + return Maze(grid, width, height, start_cell, exit_cell) + +#Этап 3 +class PathFindingStrategy(ABC): + @abstractmethod + def findPath(self,maze, start, exit): + pass + +class BFS(PathFindingStrategy): + def findPath(self, maze, start, exit): + queue = deque([start]) + travled_path={start: None} + while queue: + current = queue.popleft() + if current==exit: + path=[] + while current is not None: + path.append(current) + current = travled_path[current] + return path[::-1] + for neighbor in maze.getNeighbors(current): + if neighbor not in travled_path: + travled_path[neighbor] = current + queue.append(neighbor) + return [] + +class DFS(PathFindingStrategy): + def findPath(self, maze, start, exit):