from typing import List, Optional class Cell: """Клетка лабиринта""" def __init__(self, x: int, y: int): self.x = x self.y = y self.is_wall = False self.is_start = False self.is_exit = False def is_passable(self) -> bool: """Проверяет, можно ли пройти через клетку""" return not self.is_wall def __eq__(self, other) -> bool: if not isinstance(other, Cell): return False return self.x == other.x and self.y == other.y def __hash__(self): return hash((self.x, self.y)) def __repr__(self): return f"Cell({self.x}, {self.y})" class Maze: """Лабиринт (сетка клеток)""" def __init__(self, width: int, height: int): self.width = width self.height = height self._cells: List[List[Optional[Cell]]] = [[None for _ in range(width)] for _ in range(height)] self.start: Optional[Cell] = None self.exit: Optional[Cell] = None def set_cell(self, x: int, y: int, cell: Cell) -> None: """Устанавливает клетку в указанные координаты""" if 0 <= x < self.width and 0 <= y < self.height: self._cells[y][x] = cell if cell.is_start: self.start = cell if cell.is_exit: self.exit = cell def get_cell(self, x: int, y: int) -> Optional[Cell]: """Возвращает клетку по координатам""" if 0 <= x < self.width and 0 <= y < self.height: return self._cells[y][x] return None def get_neighbors(self, cell: Cell) -> List[Cell]: """Возвращает список соседних проходимых клеток""" neighbors = [] directions = [(0, -1), (0, 1), (-1, 0), (1, 0)] # вверх, вниз, влево, вправо for dx, dy in directions: nx, ny = cell.x + dx, cell.y + dy neighbor = self.get_cell(nx, ny) if neighbor and neighbor.is_passable(): neighbors.append(neighbor) return neighbors def __str__(self) -> str: """Строковое представление лабиринта""" result = [] for y in range(self.height): row = [] for x in range(self.width): cell = self.get_cell(x, y) if cell is None: row.append('?') elif cell.is_start: row.append('S') elif cell.is_exit: row.append('E') elif cell.is_wall: row.append('#') else: row.append(' ') result.append(''.join(row)) return '\n'.join(result)