diff --git a/ProninVV/task-2-oop/DepthFirstSearch.py b/ProninVV/task-2-oop/DepthFirstSearch.py new file mode 100644 index 0000000..5f0f2c0 --- /dev/null +++ b/ProninVV/task-2-oop/DepthFirstSearch.py @@ -0,0 +1,33 @@ +from strategy import PathFindingStrategy +from Maze import Maze, Cell + + +class DFSStrategy(PathFindingStrategy): + def findPath(maze: Maze, start, exit): + + if not start or not exit: + return [] + + visited = set() + path = [] + + def dfs(root: Cell) -> bool: + visited.add(root) + path.append(root) + + if root == exit: + return True + + neighbors = maze.getNeighbors(root) + for neighbor in neighbors: + if neighbor not in visited: + if dfs(neighbor): + return True + + path.pop() + return False + + if dfs(start): + return path + + return []