def _breadth_first_search(self, start, end): """ Solves a maze using breadth-first search. :param start: tuple with start coordinates :param end: tuple with end coordinates :return: None """ visited = self.maze.copy( ) # List of visited cells, value of visited cell is 0 queue = collections.deque() # List of cells [cell, ...] cell = utils.stack_empty( ) # Tuple of current cell with according stack ((x, y), stack) x, y = start cell = utils.stack_push(cell, (x, y)) queue.append(cell) visited[x, y, 0] = 0 # Mark as visited while queue: self._enqueue(queue, visited) if queue[0][0] == end: # Stop if end has been found cell = utils.stack_push(queue[0], end) # Push end into cell return utils.draw_path(self.solution, utils.stack_deque(cell)) raise utils.MazeError("No solution found.")
def _depth_first_search(self, start, end): """Solves a maze using depth-first search.""" visited = self.maze.copy() # List of visited cells, value of visited cell is 0 stack = collections.deque() # List of visited cells [(x, y), ...] x, y = start visited[x, y, 0] = 0 # Mark as visited while x and y: while x and y: stack.append((x, y)) if (x, y) == end: # Stop if end has been found return utils.draw_path(self.solution, stack) x, y = self._solve_walk(x, y, visited) x, y = self._solve_backtrack(stack, visited) raise utils.MazeError("No solution found.")