исправлены алгоритмы поиска

This commit is contained in:
Proninvv 2026-05-20 22:49:33 +03:00
parent 8a6344f301
commit 324333c9aa
4 changed files with 22 additions and 17 deletions

View File

@ -3,18 +3,18 @@ from strategy import PathFindingStrategy
class AStarStrategy(PathFindingStrategy):
def findPath(maze, start, exit):
def findPath(self, maze, start, exit):
def heuristik(cell):
return abs(cell.x - exit.x) + abs(cell.y - exit.y)
if not start or not exit:
return []
parents = {start: None}
queue = [start]
if not start or not exit:
return [], 0
while len(queue) != 0:
best_cell = queue[0]
for cell in queue:
@ -38,4 +38,4 @@ class AStarStrategy(PathFindingStrategy):
if child not in parents:
parents[child] = u
queue.append(child)
return []
return [], len(parents)

View File

@ -3,15 +3,16 @@ from Maze import Maze, Cell
class BFSStrategy(PathFindingStrategy):
def findPath(maze: Maze, start: Cell, exit: Cell):
def findPath(self, maze: Maze, start: Cell, exit: Cell):
if not start or not exit:
return []
# очерель: перывй вошел - первый вышел
queue = [start]
# будем хранить откуда в какую клетку пришли
parents = {start: None}
if not start or not exit:
return [], 0
while (len(queue) != 0):
u = queue.pop(0)
if u == exit:
@ -28,4 +29,4 @@ class BFSStrategy(PathFindingStrategy):
if child not in parents:
parents[child] = u
queue.append(child)
return []
return [], len(parents)

View File

@ -3,10 +3,10 @@ from Maze import Maze, Cell
class DeikstraFind(PathFindingStrategy):
def findPath(maze, start, exit):
def findPath(self, maze, start, exit):
if not start or not exit:
return []
return [], len(parents)
queue = [start]
@ -40,4 +40,4 @@ class DeikstraFind(PathFindingStrategy):
if child not in queue:
queue.append(child)
return []
return [], len(parents)

View File

@ -1,12 +1,16 @@
import sys
from strategy import PathFindingStrategy
from Maze import Maze, Cell
sys.setrecursionlimit(15000)
class DFSStrategy(PathFindingStrategy):
def findPath(maze: Maze, start, exit):
def findPath(self, maze: Maze, start, exit):
if not start or not exit:
return []
return [], 0
visited = set()
path = []
@ -16,7 +20,7 @@ class DFSStrategy(PathFindingStrategy):
def dfs(root: Cell) -> bool:
visited.add(root)
path.append(root)
count_cell += 1
# count_cell += 1
if root == exit:
return True
@ -31,6 +35,6 @@ class DFSStrategy(PathFindingStrategy):
return False
if dfs(start):
return path, count_cell
return path, len(visited)
return []
return [], len(visited)