コード例 #1
0
ファイル: maze.py プロジェクト: trydyingtolive/youreamazing
    def _breadth_first_search(self, start, end):
        """Solves a maze using breadth-first search."""
        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.")
コード例 #2
0
ファイル: maze.py プロジェクト: trydyingtolive/youreamazing
 def _enqueue(self, queue, visited):
     """Queues next cells."""
     cell = queue.popleft()
     x, y = cell[0]
     for idx in range(4):  # Check adjacent cells
         bx, by = self._dir_one[idx](x, y)
         if visited[bx, by, 0] == 255:  # Check if unvisited
             tx, ty = self._dir_two[idx](x, y)
             visited[bx, by, 0] = visited[tx, ty, 0] = 0  # Mark as visited
             queue.append(utils.stack_push(cell, (tx, ty)))