class Cell:
    """Представляет одну клетку лабиринта."""

    def __init__(self, x: int, y: int, is_wall: bool = False,
                 is_start: bool = False, is_exit: bool = False):
        self.x = x
        self.y = y
        self.is_wall = is_wall
        self.is_start = is_start
        self.is_exit = is_exit

    def is_passable(self) -> bool:
        """True, если клетка проходима (не стена)."""
        return not self.is_wall

    def __repr__(self):
        if self.is_start:
            return "S"
        if self.is_exit:
            return "E"
        return "#" if self.is_wall else "."

    def __eq__(self, other):
        return isinstance(other, Cell) and self.x == other.x and self.y == other.y

    def __hash__(self):
        return hash((self.x, self.y))


class Maze:
    """Хранит двумерную сетку клеток, размеры и ссылки на старт/выход."""

    def __init__(self, width: int, height: int, cells: list[list[Cell]],
                 start: Cell, exit_cell: Cell):
        self.width = width
        self.height = height
        self.cells = cells          # cells[y][x]
        self.start = start
        self.exit = exit_cell

    def get_cell(self, x: int, y: int) -> Cell:
        return self.cells[y][x]

    def get_neighbors(self, cell: Cell) -> list[Cell]:
        """Возвращает список проходимых соседей (вверх, вниз, влево, вправо)."""
        neighbors = []
        for dx, dy in [(0, -1), (0, 1), (-1, 0), (1, 0)]:
            nx, ny = cell.x + dx, cell.y + dy
            if 0 <= nx < self.width and 0 <= ny < self.height:
                neighbor = self.cells[ny][nx]
                if neighbor.is_passable():
                    neighbors.append(neighbor)
        return neighbors

    def __repr__(self):
        lines = []
        for row in self.cells:
            lines.append("".join(str(c) for c in row))
        return "\n".join(lines)
    
    
class Player:
    def __init__(self, start_cell):
        self.current = start_cell

    def place(self, cell):
        self.current = cell

    def getPosition(self):
        return self.current.getPosition()

    def __str__(self):
        x, y = self.getPosition()
        return f"Player({x}, {y})"
