2026-rff_mp/romanovpv/task 2/docs/data/strategies.py

73 lines
2.7 KiB
Python
Raw Normal View History

2026-05-18 10:39:13 +00:00
from abc import ABC, abstractmethod
from collections import deque
import heapq
class PathFindingStrategy(ABC):
@abstractmethod
def findPath(self, maze, start, exit_cell):
pass
2026-05-24 19:18:38 +00:00
def restorePath(self, parent, start, exit_cell):
2026-05-18 10:39:13 +00:00
path = []
current = exit_cell
while current != start:
path.append(current)
current = parent[current]
path.append(start)
path.reverse()
return path
class BFSStrategy(PathFindingStrategy):
2026-05-24 19:18:38 +00:00
def findPath( self, maze, start, exit_cell):
2026-05-18 10:39:13 +00:00
queue = deque([start])
visited = {start}
parent = {}
while queue:
current = queue.popleft()
if current == exit_cell:
2026-05-24 19:18:38 +00:00
return (self.restorePath(parent, start, exit_cell), len(visited))
2026-05-18 10:39:13 +00:00
for neighbor in maze.getNeighbors(current):
if neighbor not in visited:
2026-05-24 19:18:38 +00:00
visited.add(neighbor)
parent[neighbor] = current
queue.append(neighbor)
2026-05-18 19:10:10 +00:00
return [], len(visited)
2026-05-18 10:39:13 +00:00
class DFSStrategy(PathFindingStrategy):
2026-05-24 19:18:38 +00:00
def findPath(self, maze, start, exit_cell):
2026-05-18 10:39:13 +00:00
stack = [start]
visited = {start}
parent = {}
while stack:
current = stack.pop()
if current == exit_cell:
2026-05-24 19:18:38 +00:00
return (self.restorePath(parent,start,exit_cell),len(visited))
2026-05-18 10:39:13 +00:00
for neighbor in maze.getNeighbors(current):
if neighbor not in visited:
2026-05-24 19:18:38 +00:00
visited.add(neighbor)
parent[neighbor] = current
stack.append(neighbor)
2026-05-18 19:10:10 +00:00
return [], len(visited)
2026-05-18 10:39:13 +00:00
class AStarStrategy(PathFindingStrategy):
2026-05-24 19:18:38 +00:00
def heuristic(self,cell,exit_cell):
2026-05-18 19:10:10 +00:00
return (abs(cell.x - exit_cell.x) + abs(cell.y - exit_cell.y))
2026-05-24 19:18:38 +00:00
def findPath(self, maze, start, exit_cell):
2026-05-18 10:39:13 +00:00
pq = []
2026-05-24 19:18:38 +00:00
heapq.heappush(pq,(0, id(start), start))
2026-05-18 10:39:13 +00:00
parent = {}
2026-05-24 19:18:38 +00:00
g_score = {start: 0}
2026-05-18 10:39:13 +00:00
visited = set()
while pq:
2026-05-24 19:18:38 +00:00
_, _, current = (heapq.heappop(pq))
2026-05-18 10:39:13 +00:00
if current in visited:
continue
2026-05-24 19:18:38 +00:00
visited.add(current)
2026-05-18 10:39:13 +00:00
if current == exit_cell:
2026-05-24 19:18:38 +00:00
return (self.restorePath(parent, start, exit_cell), len(visited))
2026-05-18 10:39:13 +00:00
for neighbor in maze.getNeighbors(current):
2026-05-24 19:18:38 +00:00
new_cost = (g_score[current]+ 1)
2026-05-18 19:10:10 +00:00
if (neighbor not in g_score or new_cost < g_score[neighbor] ):
2026-05-24 19:18:38 +00:00
g_score[neighbor] = new_cost
parent[neighbor] = current
priority = (new_cost + self.heuristic(neighbor, exit_cell))
heapq.heappush(pq,(priority,id(neighbor),neighbor))
2026-05-18 19:10:10 +00:00
return [], len(visited)