【问题标题】:Path finding in 2D map with python使用python在2D地图中寻找路径
【发布时间】:2020-03-25 11:01:07
【问题描述】:

我得到了一个标签图形式的迷宫(矩阵像素的值为 1 或 0)。我无法穿过值为 0 的像素。给定一个起点 (x1,y1) 和一个终点 (x2,y2),我必须使用 python 找到这两个点之间的所有可能路径。有没有办法做到这一点?有没有一种算法可以让我找到所有可能的路径并保存它们?

谢谢!

【问题讨论】:

    标签: python path maze


    【解决方案1】:

    按照基于modifying C++ routine 的 Python 代码计算路径。

    代码

    # Check if cell (x, y) is valid or not
    def is_valid_cell(x, y, N):
        if x < 0 or y < 0 or x >= N or y >= N:
            return False
    
        return True
    
    def find_paths_util(maze, source, destination, visited, path, paths):
      """Find paths using Breadth First Search algorith """
      # Done if destination is found
      if source == destination:
        paths.append(path[:])  # append copy of current path
        return
    
      # mark current cell as visited
      N = len(maze)
      x, y = source
      visited[x][y] = True
    
      # if current cell is a valid and open cell, 
      if is_valid_cell(x, y, N) and maze[x][y]:
        # Using Breadth First Search on path extension in all direction
    
        # go right (x, y) --> (x + 1, y)
        if x + 1 < N and (not visited[x + 1][y]):
          path.append((x + 1, y))
          find_paths_util(maze,(x + 1, y), destination, visited, path, paths)
          path.pop()
    
        # go left (x, y) --> (x - 1, y)
        if x - 1 >= 0 and (not visited[x - 1][y]):
          path.append((x - 1, y))
          find_paths_util(maze, (x - 1, y), destination, visited, path, paths)
          path.pop()
    
        # go up (x, y) --> (x, y + 1)
        if y + 1 < N and (not visited[x][y + 1]):
          path.append((x, y + 1))
          find_paths_util(maze, (x, y + 1), destination, visited, path, paths)
          path.pop()
    
        # go down (x, y) --> (x, y - 1)
        if y - 1 >= 0 and (not visited[x][y - 1]):
          path.append((x, y - 1))
          find_paths_util(maze, (x, y - 1), destination, visited, path, paths)
          path.pop()
    
        # Unmark current cell as visited
      visited[x][y] = False
    
      return paths
    
    def find_paths(maze, source, destination):
      """ Sets up and searches for paths"""
      N = len(maze) # size of Maze is N x N
    
      # 2D matrix to keep track of cells involved in current path
      visited = [[False]*N for _ in range(N)]
    
      path = [source]
      paths = []
      paths = find_paths_util(maze, source, destination, visited, path, paths)
    
      return paths
    

    测试

    # Test Maze
    maze = [
      [1, 1, 1, 1],
      [1, 1, 0, 1],
      [0, 1, 0, 1],
      [1, 1, 1, 1]
    ]
    N = len(maze)
    
    # Start point and destination
    source = (0, 0)  # top left corner
    destination = (N-1, N-1)  # bottom right corner
    
    # Find all paths
    paths = find_paths(maze, source, destination)
    
    print("Paths with '->' separator between maze cell locations")
    for path in paths:
      print(*path, sep = ' -> ')
    

    输出

    Paths with '->' separator between maze cell locations
    (0, 0) -> (1, 0) -> (1, 1) -> (2, 1) -> (3, 1) -> (3, 2) -> (3, 3)
    (0, 0) -> (1, 0) -> (1, 1) -> (0, 1) -> (0, 2) -> (0, 3) -> (1, 3) -> (2, 3) -> (3, 3)
    (0, 0) -> (0, 1) -> (1, 1) -> (2, 1) -> (3, 1) -> (3, 2) -> (3, 3)
    (0, 0) -> (0, 1) -> (0, 2) -> (0, 3) -> (1, 3) -> (2, 3) -> (3, 3)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-25
      • 2013-04-03
      相关资源
      最近更新 更多