From 2449eeb26f437a62d9f2967fa40abe5a23d329c6 Mon Sep 17 00:00:00 2001 From: Proninvv Date: Tue, 19 May 2026 18:17:35 +0300 Subject: [PATCH] =?UTF-8?q?=D1=80=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=BE?= =?UTF-8?q?=D0=B2=D0=B0=D0=BD=20=D0=BF=D0=BE=D0=B8=D1=81=D0=BA=20=D0=B2=20?= =?UTF-8?q?=D0=B3=D0=BB=D1=83=D0=B1=D0=B8=D0=BD=D1=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ProninVV/task-2-oop/DepthFirstSearch.py | 33 +++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 ProninVV/task-2-oop/DepthFirstSearch.py 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 []