2026-rff_mp/YanyaevAA/task2/task_2.py

164 lines
5.1 KiB
Python
Raw Normal View History

from abc import ABC, abstractmethod
from collections import deque
2026-05-14 15:51:29 +00:00
import heapq
#Этап 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])
2026-05-14 15:51:29 +00:00
traveled_path={start: None}
while queue:
current = queue.popleft()
if current==exit:
path=[]
while current is not None:
path.append(current)
2026-05-14 15:51:29 +00:00
current = traveled_path[current]
return path[::-1], len(traveled_path)
for neighbor in maze.getNeighbors(current):
2026-05-14 15:51:29 +00:00
if neighbor not in traveled_path:
traveled_path[neighbor] = current
queue.append(neighbor)
2026-05-14 15:51:29 +00:00
return [], len(traveled_path)
class DFS(PathFindingStrategy):
def findPath(self, maze, start, exit):
2026-05-14 15:51:29 +00:00
stack = [start]
traveled_path={start: None}
while stack:
current = stack.pop()
if current == exit:
path = []
while current is not None:
path.append(current)
current = traveled_path[current]
return path[::-1], len(traveled_path)
for neighbor in maze.getNeighbors(current):
if neighbor not in traveled_path:
traveled_path[neighbor] = current
stack.append(neighbor)
return [], len(traveled_path)
class AStar(PathFindingStrategy):
def findPath(self, maze, start, exit):
count = 0
open_set = [(0, count, start)]
traveled_path = {start: None}
g_score = {start: 0}
while open_set:
_,_,current = heapq.heappop(open_set)
if current == exit:
path = []
while current is not None:
path.append(current)
current = traveled_path[current]
return path[::-1], len(traveled_path)
for neighbor in maze.getNeighbors(current):
g_score_new = g_score[current]+1
if neighbor not in g_score or g_score_new < g_score[neighbor]:
traveled_path[neighbor] = current
g_score[neighbor] = g_score_new
f_score = tentative_g_score + abs(neighbor.x - exit.x) + abs(neighbor.y - exit.y)
count += 1
heapq.heappush(open_set, (f_score, count, neighbor))
return [],len(traveled_path)
#Этап 4
class MazeSolver:
def __init__(self, maze, strategy):
self.maze = maze
self.strategy = strategy
def setStrategy(self, strategy):
self.strategy = strategy
def solve(self):
start_cell = self.maze.getStart()
exit_cell = self.maze.getExit()
start_time = time.perf_counter()
path, visited_cells = self.strategy.findPath(self.maze, start_cell, exit_cell)
end_time = time.perf_counter()
time_ms = (end_time - start_time) * 1000
path_length = len(path)
return time_ms, visited_cells, path_length
#Этап 5