Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
基于python的二维地图路径搜索_Python_Path_Maze - Fatal编程技术网

基于python的二维地图路径搜索

基于python的二维地图路径搜索,python,path,maze,Python,Path,Maze,我得到了一个labelmap形式的迷宫(矩阵像素的值为1或0)。我不能与值为0的像素交叉。给定一个起点(x1,y1)和一个终点(x2,y2),我必须使用python找到这两点之间所有可能的路径。有办法做到这一点吗?有没有一种算法可以让我找到所有可能的路径并保存它们 谢谢大家! 下面是基于的Python代码,用于计算路径 代码 # Check if cell (x, y) is valid or not def is_valid_cell(x, y, N): if x < 0 or

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


谢谢大家!

下面是基于的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)