forked from UNN/2026-rff_mp
48 lines
1.4 KiB
Python
48 lines
1.4 KiB
Python
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
|
|
|
|
def __hash__(self) -> int:
|
|
return hash((self.x, self.y))
|
|
|
|
def __eq__(self, other):
|
|
if not isinstance(other, Cell):
|
|
return False
|
|
return self.x == other.x and self.y == other.y
|
|
|
|
def __lt__(self, other):
|
|
if not isinstance(other, Cell):
|
|
return NotImplemented
|
|
return (self.x, self.y) < (other.x, other.y)
|
|
|
|
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 |