forked from UNN/2026-rff_mp
Выполнены этапы 1, 2, в 3 реализовано BFS
This commit is contained in:
parent
de62717491
commit
a830e4d7a2
100
YanyaevAA/task2/task_2.py
Normal file
100
YanyaevAA/task2/task_2.py
Normal file
|
|
@ -0,0 +1,100 @@
|
||||||
|
from abc import ABC, abstractmethod
|
||||||
|
from collections import deque
|
||||||
|
#Этап 1
|
||||||
|
class Cell:
|
||||||
|
def __init__(self, x, y, is_wall=False, is_start=False, is_exit=False):
|
||||||
|
self.x = x
|
||||||
|
self.y = y
|
||||||
|
self.is_wall = is_wall
|
||||||
|
self.is_start = is_start
|
||||||
|
self.is_exit = is_exit
|
||||||
|
|
||||||
|
def isPassable(self):
|
||||||
|
return not self.is_wall
|
||||||
|
|
||||||
|
class Maze:
|
||||||
|
def __init__(self, cells, width, height, start, exit):
|
||||||
|
self.width = width
|
||||||
|
self.height = height
|
||||||
|
self.cells =cells
|
||||||
|
self.start = start
|
||||||
|
self.exit = exit
|
||||||
|
|
||||||
|
def getCell(self, x, y):
|
||||||
|
if 0 <= x< self.width and 0 <=y< self.height:
|
||||||
|
return self.cells[y][x]
|
||||||
|
return None
|
||||||
|
|
||||||
|
def getNeighbors(self, cell: Cell):
|
||||||
|
neighbors = []
|
||||||
|
directions = [(0, -1), (0, 1), (-1, 0), (1, 0)]
|
||||||
|
|
||||||
|
for dir_x, dir_y in directions:
|
||||||
|
neigh_x = cell.x+dir_x
|
||||||
|
neigh_y = cell.y+dir_y
|
||||||
|
neighbor = self.getCell(neigh_x, neigh_y)
|
||||||
|
if neighbor and neighbor.isPassable():
|
||||||
|
neighbors.append(neighbor)
|
||||||
|
return neighbors
|
||||||
|
|
||||||
|
#Этап 2
|
||||||
|
class MazeBuilder(ABC):
|
||||||
|
@abstractmethod
|
||||||
|
def buildFromFile(self, filename):
|
||||||
|
pass
|
||||||
|
|
||||||
|
class TextFileMazeBuilder(MazeBuilder):
|
||||||
|
def buildFromFile(self, filename):
|
||||||
|
with open(filename, 'r') as f:
|
||||||
|
lines = [line.rstrip('\n') for line in f]
|
||||||
|
height = len(lines)
|
||||||
|
width = max(len(line) for line in lines)
|
||||||
|
|
||||||
|
grid=[]
|
||||||
|
start_cell=None
|
||||||
|
exit_cell=None
|
||||||
|
for y in range(height):
|
||||||
|
row=[]
|
||||||
|
for x in range(width):
|
||||||
|
char=lines[y][x]
|
||||||
|
|
||||||
|
is_wall = (char == '#')
|
||||||
|
is_start = (char == 'S')
|
||||||
|
is_exit = (char == 'E')
|
||||||
|
|
||||||
|
cell=Cell(x, y, is_wall, is_start, is_exit)
|
||||||
|
|
||||||
|
if is_start:
|
||||||
|
start_cell =cell
|
||||||
|
if is_exit:
|
||||||
|
exit_cell =cell
|
||||||
|
row.append(cell)
|
||||||
|
grid.append(row)
|
||||||
|
return Maze(grid, width, height, start_cell, exit_cell)
|
||||||
|
|
||||||
|
#Этап 3
|
||||||
|
class PathFindingStrategy(ABC):
|
||||||
|
@abstractmethod
|
||||||
|
def findPath(self,maze, start, exit):
|
||||||
|
pass
|
||||||
|
|
||||||
|
class BFS(PathFindingStrategy):
|
||||||
|
def findPath(self, maze, start, exit):
|
||||||
|
queue = deque([start])
|
||||||
|
travled_path={start: None}
|
||||||
|
while queue:
|
||||||
|
current = queue.popleft()
|
||||||
|
if current==exit:
|
||||||
|
path=[]
|
||||||
|
while current is not None:
|
||||||
|
path.append(current)
|
||||||
|
current = travled_path[current]
|
||||||
|
return path[::-1]
|
||||||
|
for neighbor in maze.getNeighbors(current):
|
||||||
|
if neighbor not in travled_path:
|
||||||
|
travled_path[neighbor] = current
|
||||||
|
queue.append(neighbor)
|
||||||
|
return []
|
||||||
|
|
||||||
|
class DFS(PathFindingStrategy):
|
||||||
|
def findPath(self, maze, start, exit):
|
||||||
Loading…
Reference in New Issue
Block a user