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,
AStarStrategy
)
from solver import (MazeSolver)
builder = TextFileMazeBuilder()
maze = builder.buildFromFile("maze.txt")
strategy = DFSStrategy()
path = strategy.findPath(maze, maze.start, maze.exit)
print(f"Метод: {strategy}")
print("Путь:\n")
for cell in path:
print(f"({cell.x},{cell.y})")
maze.printMaze()
print("Выберете алгоритм")
print("1 - BFS")
print("2 - DFS")
print("3 - A*")
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:
current = queue.popleft()
if current == exit_cell:
return self.restorePath(
return (
self.restorePath(
parent,
start,
exit_cell
exit_cell),
len(visited)
)
for neighbor in maze.getNeighbors(current):
if neighbor not in visited:
@ -49,7 +51,7 @@ class BFSStrategy(PathFindingStrategy):
queue.append(
neighbor
)
return []
return [], len(visited)
class DFSStrategy(PathFindingStrategy):
def findPath(
@ -64,10 +66,12 @@ class DFSStrategy(PathFindingStrategy):
while stack:
current = stack.pop()
if current == exit_cell:
return self.restorePath(
return (self.restorePath
(
parent,
start,
exit_cell
exit_cell),
len(visited)
)
for neighbor in maze.getNeighbors(current):
if neighbor not in visited:
@ -81,7 +85,7 @@ class DFSStrategy(PathFindingStrategy):
stack.append(
neighbor
)
return []
return [], len(visited)
class AStarStrategy(PathFindingStrategy):
def heuristic(
@ -89,17 +93,7 @@ class AStarStrategy(PathFindingStrategy):
cell,
exit_cell
):
return (
abs(
cell.x
- exit_cell.x
)
+
abs(
cell.y
- exit_cell.y
)
)
return (abs(cell.x - exit_cell.x) + abs(cell.y - exit_cell.y))
def findPath(
self,
maze,
@ -132,36 +126,24 @@ class AStarStrategy(PathFindingStrategy):
current
)
if current == exit_cell:
return self.restorePath(
return (self.restorePath(
parent,
start,
exit_cell
exit_cell),
len(visited)
)
for neighbor in maze.getNeighbors(current):
new_cost = (
g_score[current]
+ 1
)
if (
neighbor
not in g_score
or
new_cost <
g_score[neighbor]
):
g_score[
neighbor
if (neighbor not in g_score or new_cost < g_score[neighbor] ):
g_score[neighbor
] = new_cost
parent[
neighbor
] = current
priority = (
new_cost
+
self.heuristic(
neighbor,
exit_cell
)
priority = (new_cost + self.heuristic(neighbor, exit_cell)
)
heapq.heappush(
pq,
@ -171,4 +153,4 @@ class AStarStrategy(PathFindingStrategy):
neighbor
)
)
return []
return [], len(visited)