Добавлены 3, 4 этапы

This commit is contained in:
yanyaevaa 2026-05-14 18:51:29 +03:00
parent a830e4d7a2
commit e77bf45914

View File

@ -1,5 +1,6 @@
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
from collections import deque from collections import deque
import heapq
#Этап 1 #Этап 1
class Cell: class Cell:
def __init__(self, x, y, is_wall=False, is_start=False, is_exit=False): def __init__(self, x, y, is_wall=False, is_start=False, is_exit=False):
@ -81,20 +82,83 @@ class PathFindingStrategy(ABC):
class BFS(PathFindingStrategy): class BFS(PathFindingStrategy):
def findPath(self, maze, start, exit): def findPath(self, maze, start, exit):
queue = deque([start]) queue = deque([start])
travled_path={start: None} traveled_path={start: None}
while queue: while queue:
current = queue.popleft() current = queue.popleft()
if current==exit: if current==exit:
path=[] path=[]
while current is not None: while current is not None:
path.append(current) path.append(current)
current = travled_path[current] current = traveled_path[current]
return path[::-1] return path[::-1], len(traveled_path)
for neighbor in maze.getNeighbors(current): for neighbor in maze.getNeighbors(current):
if neighbor not in travled_path: if neighbor not in traveled_path:
travled_path[neighbor] = current traveled_path[neighbor] = current
queue.append(neighbor) queue.append(neighbor)
return [] return [], len(traveled_path)
class DFS(PathFindingStrategy): class DFS(PathFindingStrategy):
def findPath(self, maze, start, exit): def findPath(self, maze, start, exit):
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