116 lines
2.8 KiB
Python
116 lines
2.8 KiB
Python
|
|
import sys
|
||
|
|
from collections import deque
|
||
|
|
import heapq
|
||
|
|
import time
|
||
|
|
import os
|
||
|
|
import csv
|
||
|
|
import matplotlib.pyplot as plt
|
||
|
|
import numpy as np
|
||
|
|
|
||
|
|
|
||
|
|
# ----------------------------- Модель клетки -----------------------------
|
||
|
|
class GridCell:
|
||
|
|
def __init__(self, x, y):
|
||
|
|
self._x = x
|
||
|
|
self._y = y
|
||
|
|
self._blocked = False # стена
|
||
|
|
self._entry = False # старт
|
||
|
|
self._exit_flag = False # выход
|
||
|
|
|
||
|
|
@property
|
||
|
|
def x(self):
|
||
|
|
return self._x
|
||
|
|
|
||
|
|
@property
|
||
|
|
def y(self):
|
||
|
|
return self._y
|
||
|
|
|
||
|
|
@property
|
||
|
|
def is_wall(self):
|
||
|
|
return self._blocked
|
||
|
|
|
||
|
|
@is_wall.setter
|
||
|
|
def is_wall(self, value):
|
||
|
|
self._blocked = value
|
||
|
|
|
||
|
|
@property
|
||
|
|
def is_start(self):
|
||
|
|
return self._entry
|
||
|
|
|
||
|
|
@is_start.setter
|
||
|
|
def is_start(self, value):
|
||
|
|
self._entry = value
|
||
|
|
|
||
|
|
@property
|
||
|
|
def is_exit(self):
|
||
|
|
return self._exit_flag
|
||
|
|
|
||
|
|
@is_exit.setter
|
||
|
|
def is_exit(self, value):
|
||
|
|
self._exit_flag = value
|
||
|
|
|
||
|
|
def passable(self):
|
||
|
|
return not self._blocked
|
||
|
|
|
||
|
|
|
||
|
|
# ----------------------------- Модель лабиринта -----------------------------
|
||
|
|
class Labyrinth:
|
||
|
|
def __init__(self, width, height):
|
||
|
|
self._width = width
|
||
|
|
self._height = height
|
||
|
|
self._cells = [[GridCell(x, y) for x in range(width)] for y in range(height)]
|
||
|
|
self._start_cell = None
|
||
|
|
self._exit_cell = None
|
||
|
|
|
||
|
|
@property
|
||
|
|
def width(self):
|
||
|
|
return self._width
|
||
|
|
|
||
|
|
@property
|
||
|
|
def height(self):
|
||
|
|
return self._height
|
||
|
|
|
||
|
|
@property
|
||
|
|
def start(self):
|
||
|
|
return self._start_cell
|
||
|
|
|
||
|
|
@property
|
||
|
|
def exit(self):
|
||
|
|
return self._exit_cell
|
||
|
|
|
||
|
|
def cell_at(self, x, y):
|
||
|
|
if 0 <= x < self._width and 0 <= y < self._height:
|
||
|
|
return self._cells[y][x]
|
||
|
|
return None
|
||
|
|
|
||
|
|
def configure_cell(self, x, y, cell_type):
|
||
|
|
cell = self.cell_at(x, y)
|
||
|
|
if cell is None:
|
||
|
|
return
|
||
|
|
|
||
|
|
if cell_type == 'wall':
|
||
|
|
cell.is_wall = True
|
||
|
|
elif cell_type == 'start':
|
||
|
|
if self._start_cell:
|
||
|
|
self._start_cell.is_start = False
|
||
|
|
cell.is_start = True
|
||
|
|
cell.is_wall = False
|
||
|
|
self._start_cell = cell
|
||
|
|
elif cell_type == 'exit':
|
||
|
|
if self._exit_cell:
|
||
|
|
self._exit_cell.is_exit = False
|
||
|
|
cell.is_exit = True
|
||
|
|
cell.is_wall = False
|
||
|
|
self._exit_cell = cell
|
||
|
|
elif cell_type == 'path':
|
||
|
|
cell.is_wall = False
|
||
|
|
|
||
|
|
def adjacent_cells(self, cell):
|
||
|
|
neighbours = []
|
||
|
|
directions = [(0, -1), (0, 1), (-1, 0), (1, 0)]
|
||
|
|
for dx, dy in directions:
|
||
|
|
nx, ny = cell.x + dx, cell.y + dy
|
||
|
|
neighbour = self.cell_at(nx, ny)
|
||
|
|
if neighbour and neighbour.passable():
|
||
|
|
neighbours.append(neighbour)
|
||
|
|
return neighbours
|