forked from UNN/2026-rff_mp
[2] Стратегии поиска пути
This commit is contained in:
parent
4422dcb9e7
commit
047c2baf6d
|
|
@ -120,3 +120,114 @@ class TextileMazeBuilder(MazeBuilder):
|
|||
|
||||
return maze
|
||||
|
||||
class PathFindingStrategy:
|
||||
def findPath(self, maze, start, exit):
|
||||
pass
|
||||
|
||||
|
||||
class BFSStrategy(PathFindingStrategy):
|
||||
def findPath(self, maze, start, exit):
|
||||
if exit is None:
|
||||
return []
|
||||
queue = deque([start])
|
||||
visited = {start}
|
||||
parent = {start: None}
|
||||
|
||||
while queue:
|
||||
current = queue.popleft()
|
||||
|
||||
if current == exit:
|
||||
return self._reconstruct_path(parent, start, exit)
|
||||
|
||||
for neighbor in maze.getNeighbors(current):
|
||||
if neighbor not in visited:
|
||||
visited.add(neighbor)
|
||||
parent[neighbor] = current
|
||||
queue.append(neighbor)
|
||||
|
||||
return []
|
||||
|
||||
def _reconstruct_path(self, parent, start, exit):
|
||||
path = []
|
||||
current = exit
|
||||
while current is not None:
|
||||
path.append(current)
|
||||
current = parent[current]
|
||||
path.reverse()
|
||||
return path
|
||||
|
||||
|
||||
class DFSStrategy(PathFindingStrategy):
|
||||
def findPath(self, maze, start, exit):
|
||||
if exit is None:
|
||||
return []
|
||||
stack = [start]
|
||||
visited = {start}
|
||||
parent = {start: None}
|
||||
|
||||
while stack:
|
||||
current = stack.pop()
|
||||
|
||||
if current == exit:
|
||||
return self._reconstruct_path(parent, start, exit)
|
||||
|
||||
for neighbor in maze.getNeighbors(current):
|
||||
if neighbor not in visited:
|
||||
visited.add(neighbor)
|
||||
parent[neighbor] = current
|
||||
stack.append(neighbor)
|
||||
|
||||
return []
|
||||
|
||||
def _reconstruct_path(self, parent, start, exit):
|
||||
path = []
|
||||
current = exit
|
||||
while current is not None:
|
||||
path.append(current)
|
||||
current = parent[current]
|
||||
path.reverse()
|
||||
return path
|
||||
|
||||
|
||||
class AStrategy(PathFindingStrategy):
|
||||
def _heuristic(self, cell, exit):
|
||||
if exit is None:
|
||||
return 0
|
||||
return abs(cell.x - exit.x) + abs(cell.y - exit.y)
|
||||
|
||||
def findPath(self, maze, start, exit):
|
||||
if exit is None:
|
||||
return []
|
||||
open_set = []
|
||||
heapq.heappush(open_set, (0, start))
|
||||
|
||||
came_from = {start: None}
|
||||
g_score = {start: 0}
|
||||
|
||||
while open_set:
|
||||
current = heapq.heappop(open_set)[1]
|
||||
|
||||
if current == exit:
|
||||
return self._reconstruct_path(came_from, start, exit)
|
||||
|
||||
for neighbor in maze.getNeighbors(current):
|
||||
tentative_g = g_score[current] + 1
|
||||
|
||||
if neighbor not in g_score or tentative_g < g_score[neighbor]:
|
||||
came_from[neighbor] = current
|
||||
g_score[neighbor] = tentative_g
|
||||
f_score = tentative_g + self._heuristic(neighbor, exit)
|
||||
heapq.heappush(open_set, (f_score, neighbor))
|
||||
|
||||
return []
|
||||
|
||||
def _reconstruct_path(self, came_from, start, exit):
|
||||
path = []
|
||||
current = exit
|
||||
while current is not None:
|
||||
path.append(current)
|
||||
current = came_from[current]
|
||||
path.reverse()
|
||||
return path
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user