This commit is contained in:
Pavel 2026-05-18 22:10:10 +03:00
parent dabd0acd9e
commit d0b791287f
3 changed files with 102 additions and 42 deletions

View File

@ -5,12 +5,28 @@ from strategies import (
DFSStrategy, DFSStrategy,
AStarStrategy AStarStrategy
) )
from solver import (MazeSolver)
builder = TextFileMazeBuilder() builder = TextFileMazeBuilder()
maze = builder.buildFromFile("maze.txt") maze = builder.buildFromFile("maze.txt")
strategy = DFSStrategy() maze.printMaze()
path = strategy.findPath(maze, maze.start, maze.exit) print("Выберете алгоритм")
print(f"Метод: {strategy}") print("1 - BFS")
print("Путь:\n") print("2 - DFS")
for cell in path: print("3 - A*")
print(f"({cell.x},{cell.y})") choice = input()
if choice == "1":
strategy = BFSStrategy()
elif choice == "2":
strategy = DFSStrategy()
elif choice == "3":
strategy = AStarStrategy()
else:
print("Неверный выбор")
exit()
solver = MazeSolver(maze, strategy)
stats = solver.solve()
print("Результат:")
print(stats)

View File

@ -0,0 +1,62 @@
import time
class SearchStats:
def __init__(
self,
time_ms,
visited_cells,
path_length
):
self.time_ms = time_ms
self.visited_cells = visited_cells
self.path_length = path_length
def __str__(self):
return (
f"Время: "
f"{self.time_ms:.3f} мс\n"
f"Посещено клеток: "
f"{self.visited_cells}\n"
f"Длина пути: "
f"{self.path_length}"
)
class MazeSolver:
def __init__(
self,
maze,
strategy
):
self.maze = maze
self.strategy = strategy
def setStrategy(
self,
strategy
):
self.strategy = strategy
def solve(self):
start_time = (
time.perf_counter()
)
path, visited = (
self.strategy.findPath(
self.maze,
self.maze.start,
self.maze.exit
)
)
end_time = (
time.perf_counter()
)
time_ms = (
(end_time-start_time)
*1000
)
visited = len(path)
stats = SearchStats(
time_ms,
visited,
len(path)
)
return stats

View File

@ -33,10 +33,12 @@ class BFSStrategy(PathFindingStrategy):
while queue: while queue:
current = queue.popleft() current = queue.popleft()
if current == exit_cell: if current == exit_cell:
return self.restorePath( return (
self.restorePath(
parent, parent,
start, start,
exit_cell exit_cell),
len(visited)
) )
for neighbor in maze.getNeighbors(current): for neighbor in maze.getNeighbors(current):
if neighbor not in visited: if neighbor not in visited:
@ -49,7 +51,7 @@ class BFSStrategy(PathFindingStrategy):
queue.append( queue.append(
neighbor neighbor
) )
return [] return [], len(visited)
class DFSStrategy(PathFindingStrategy): class DFSStrategy(PathFindingStrategy):
def findPath( def findPath(
@ -64,10 +66,12 @@ class DFSStrategy(PathFindingStrategy):
while stack: while stack:
current = stack.pop() current = stack.pop()
if current == exit_cell: if current == exit_cell:
return self.restorePath( return (self.restorePath
(
parent, parent,
start, start,
exit_cell exit_cell),
len(visited)
) )
for neighbor in maze.getNeighbors(current): for neighbor in maze.getNeighbors(current):
if neighbor not in visited: if neighbor not in visited:
@ -81,7 +85,7 @@ class DFSStrategy(PathFindingStrategy):
stack.append( stack.append(
neighbor neighbor
) )
return [] return [], len(visited)
class AStarStrategy(PathFindingStrategy): class AStarStrategy(PathFindingStrategy):
def heuristic( def heuristic(
@ -89,17 +93,7 @@ class AStarStrategy(PathFindingStrategy):
cell, cell,
exit_cell exit_cell
): ):
return ( return (abs(cell.x - exit_cell.x) + abs(cell.y - exit_cell.y))
abs(
cell.x
- exit_cell.x
)
+
abs(
cell.y
- exit_cell.y
)
)
def findPath( def findPath(
self, self,
maze, maze,
@ -132,36 +126,24 @@ class AStarStrategy(PathFindingStrategy):
current current
) )
if current == exit_cell: if current == exit_cell:
return self.restorePath( return (self.restorePath(
parent, parent,
start, start,
exit_cell exit_cell),
len(visited)
) )
for neighbor in maze.getNeighbors(current): for neighbor in maze.getNeighbors(current):
new_cost = ( new_cost = (
g_score[current] g_score[current]
+ 1 + 1
) )
if ( if (neighbor not in g_score or new_cost < g_score[neighbor] ):
neighbor g_score[neighbor
not in g_score
or
new_cost <
g_score[neighbor]
):
g_score[
neighbor
] = new_cost ] = new_cost
parent[ parent[
neighbor neighbor
] = current ] = current
priority = ( priority = (new_cost + self.heuristic(neighbor, exit_cell)
new_cost
+
self.heuristic(
neighbor,
exit_cell
)
) )
heapq.heappush( heapq.heappush(
pq, pq,
@ -171,4 +153,4 @@ class AStarStrategy(PathFindingStrategy):
neighbor neighbor
) )
) )
return [] return [], len(visited)