forked from UNN/2026-rff_mp
исправлены алгоритмы поиска
This commit is contained in:
parent
8a6344f301
commit
324333c9aa
|
|
@ -3,18 +3,18 @@ from strategy import PathFindingStrategy
|
||||||
|
|
||||||
|
|
||||||
class AStarStrategy(PathFindingStrategy):
|
class AStarStrategy(PathFindingStrategy):
|
||||||
def findPath(maze, start, exit):
|
def findPath(self, maze, start, exit):
|
||||||
|
|
||||||
def heuristik(cell):
|
def heuristik(cell):
|
||||||
return abs(cell.x - exit.x) + abs(cell.y - exit.y)
|
return abs(cell.x - exit.x) + abs(cell.y - exit.y)
|
||||||
|
|
||||||
if not start or not exit:
|
|
||||||
return []
|
|
||||||
|
|
||||||
parents = {start: None}
|
parents = {start: None}
|
||||||
|
|
||||||
queue = [start]
|
queue = [start]
|
||||||
|
|
||||||
|
if not start or not exit:
|
||||||
|
return [], 0
|
||||||
|
|
||||||
while len(queue) != 0:
|
while len(queue) != 0:
|
||||||
best_cell = queue[0]
|
best_cell = queue[0]
|
||||||
for cell in queue:
|
for cell in queue:
|
||||||
|
|
@ -38,4 +38,4 @@ class AStarStrategy(PathFindingStrategy):
|
||||||
if child not in parents:
|
if child not in parents:
|
||||||
parents[child] = u
|
parents[child] = u
|
||||||
queue.append(child)
|
queue.append(child)
|
||||||
return []
|
return [], len(parents)
|
||||||
|
|
|
||||||
|
|
@ -3,15 +3,16 @@ from Maze import Maze, Cell
|
||||||
|
|
||||||
|
|
||||||
class BFSStrategy(PathFindingStrategy):
|
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]
|
queue = [start]
|
||||||
# будем хранить откуда в какую клетку пришли
|
# будем хранить откуда в какую клетку пришли
|
||||||
parents = {start: None}
|
parents = {start: None}
|
||||||
|
|
||||||
|
if not start or not exit:
|
||||||
|
return [], 0
|
||||||
|
|
||||||
while (len(queue) != 0):
|
while (len(queue) != 0):
|
||||||
u = queue.pop(0)
|
u = queue.pop(0)
|
||||||
if u == exit:
|
if u == exit:
|
||||||
|
|
@ -28,4 +29,4 @@ class BFSStrategy(PathFindingStrategy):
|
||||||
if child not in parents:
|
if child not in parents:
|
||||||
parents[child] = u
|
parents[child] = u
|
||||||
queue.append(child)
|
queue.append(child)
|
||||||
return []
|
return [], len(parents)
|
||||||
|
|
|
||||||
|
|
@ -3,10 +3,10 @@ from Maze import Maze, Cell
|
||||||
|
|
||||||
|
|
||||||
class DeikstraFind(PathFindingStrategy):
|
class DeikstraFind(PathFindingStrategy):
|
||||||
def findPath(maze, start, exit):
|
def findPath(self, maze, start, exit):
|
||||||
|
|
||||||
if not start or not exit:
|
if not start or not exit:
|
||||||
return []
|
return [], len(parents)
|
||||||
|
|
||||||
queue = [start]
|
queue = [start]
|
||||||
|
|
||||||
|
|
@ -40,4 +40,4 @@ class DeikstraFind(PathFindingStrategy):
|
||||||
if child not in queue:
|
if child not in queue:
|
||||||
queue.append(child)
|
queue.append(child)
|
||||||
|
|
||||||
return []
|
return [], len(parents)
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,16 @@
|
||||||
|
import sys
|
||||||
|
|
||||||
from strategy import PathFindingStrategy
|
from strategy import PathFindingStrategy
|
||||||
from Maze import Maze, Cell
|
from Maze import Maze, Cell
|
||||||
|
|
||||||
|
sys.setrecursionlimit(15000)
|
||||||
|
|
||||||
|
|
||||||
class DFSStrategy(PathFindingStrategy):
|
class DFSStrategy(PathFindingStrategy):
|
||||||
def findPath(maze: Maze, start, exit):
|
def findPath(self, maze: Maze, start, exit):
|
||||||
|
|
||||||
if not start or not exit:
|
if not start or not exit:
|
||||||
return []
|
return [], 0
|
||||||
|
|
||||||
visited = set()
|
visited = set()
|
||||||
path = []
|
path = []
|
||||||
|
|
@ -16,7 +20,7 @@ class DFSStrategy(PathFindingStrategy):
|
||||||
def dfs(root: Cell) -> bool:
|
def dfs(root: Cell) -> bool:
|
||||||
visited.add(root)
|
visited.add(root)
|
||||||
path.append(root)
|
path.append(root)
|
||||||
count_cell += 1
|
# count_cell += 1
|
||||||
|
|
||||||
if root == exit:
|
if root == exit:
|
||||||
return True
|
return True
|
||||||
|
|
@ -31,6 +35,6 @@ class DFSStrategy(PathFindingStrategy):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
if dfs(start):
|
if dfs(start):
|
||||||
return path, count_cell
|
return path, len(visited)
|
||||||
|
|
||||||
return []
|
return [], len(visited)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user