from dataclasses import dataclass from typing import List, Optional @dataclass class Cell: x: int y: int is_wall: bool = False is_start: bool = False is_exit: bool = False def is_passable(self) -> bool: return not self.is_wall class Maze: def __init__(self, width: int, height: int): self.width = width self.height = height self.cells = [[Cell(x, y) for x in range(width)] for y in range(height)] self.start: Optional[Cell] = None self.exit: Optional[Cell] = None 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 = [] for dx, dy in ((0,1), (0,-1), (1,0), (-1,0)): 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