如何提高此最短路径/快捷方式(阵列图DS)解决方案的速度?

时间:2016-09-23 03:30:15

标签: python algorithm data-structures array-algorithms

将迷宫定义为数组,其中1是墙,0是可通行区域:

Must include start node in distance, if you BFS this it will give you 21.

[0][0] is the start point.

    |
[   V
   [0, 0, 0, 0, 0, 0], 
   [1, 1, 1, 1, 1, 0], 
   [0, 0, 0, 0, 0, 0], 
   [0, 1, 1, 1, 1, 1], 
   [0, 1, 1, 1, 1, 1], 
   [0, 0, 0, 0, 0, 0]<--   [-1][-1] is the end point.
]

我们必须找到最短路径,我们可以删除一个'1'以帮助创建快捷方式。

创建最短路径的快捷方式是将[1] [0]更改为0,打开一条使距离为11的路径。

[
   [0, 0, 0, 0, 0, 0], 
-->[0, 1, 1, 1, 1, 0], 
   [0, 0, 0, 0, 0, 0], 
   [0, 1, 1, 1, 1, 1], 
   [0, 1, 1, 1, 1, 1], 
   [0, 0, 0, 0, 0, 0]
]
return 11

我的原始思维过程遍历每个元素并检查它是否= = 1,然后执行bfs将距离与最小值进行比较。

但是当然太慢了。所以我想要遍历每个元素并检查它是否为1,然后看看它是否有两个可通过的邻居,因为这似乎是唯一可能的情况,其中快捷方式是有意义的。

这是我的代码:

import copy
def bfs(maze):
    visited = set()
    queue = []
    mazeHeight = len(maze)
    mazeWidth = len(maze[0])
    queue.append(((0,0),1))

    while queue:
        yx,distance = queue.pop(0)
        y,x = yx
        visited.add(yx)
        if yx == (mazeHeight-1,mazeWidth-1):
            return distance

        if y+1 < mazeHeight:
            if not maze[y+1][x] and (y+1,x) not in visited:
                queue.append(((y+1,x),distance+1))

        if y-1 >= 0:
            if not maze[y-1][x] and (y-1,x) not in visited:
                queue.append(((y-1,x),distance+1))

        if x+1 < mazeWidth:
            if not maze[y][x+1] and (y,x+1) not in visited:
                queue.append(((y,x+1),distance+1))

        if x-1 >= 0:
            if not maze[y][x-1] and (y,x-1) not in visited:
                queue.append(((y,x-1),distance+1))

    return False

def answer(maze):
    min = bfs(maze)
    mazeHeight = len(maze)
    mazeWidth = len(maze[0])
    for y in range(mazeHeight):
        for x in range(mazeWidth):
            if maze[y][x]:
                oneNeighbors = 0
                if y+1 < mazeHeight:
                    if not maze[y+1][x]:
                        oneNeighbors += 1

                if y-1 >= 0:
                    if not maze[y-1][x]:
                        oneNeighbors += 1

                if x+1 < mazeWidth:
                    if not maze[y][x+1]:
                        oneNeighbors += 1

                if x-1 >= 0:
                    if not maze[y][x-1]:
                        oneNeighbors += 1
                if oneNeighbors == 2:
                    tmpMaze = copy.deepcopy(maze)
                    tmpMaze[y][x] = 0
                    tmpMin = bfs(tmpMaze)
                    if tmpMin < min:
                        min = tmpMin

    return min


print(answer([[0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 0], [0, 0, 0, 0, 0, 0], [0, 1, 1, 1, 1, 1], [0, 1, 1, 1, 1, 1], [0, 0, 0, 0, 0, 0]]))

有什么建议可以提高速度吗?

1 个答案:

答案 0 :(得分:1)

你似乎走在了正确的轨道上。可以考虑以下方法:

  1. 形成n x m个节点的图表,其中nm是迷宫矩阵的维度。

  2. 如果两个节点相邻0 s,则成本的边缘。如果01分隔,则两个节点之间存在的优势。

  3. (请注意,每条路径需要维护两个成本,一个是上面的zero-one成本,另一个是跟踪最小值的路径中的节点数。

    1. 然后执行BFS并仅考虑具有zero-one cost <= 1的路径。

    2. 这将为您提供线性时间算法(节点数量为线性)。

    3. 以下代码可能包含错误,但它应该让您入门。

      def bfs(maze):
          visited = set()
          queue = []
          mazeHeight = len(maze)
          mazeWidth = len(maze[0])
          queue.append(((0,0),1,0))    
      
          while queue:
              yx,distance, cost = queue.pop(0)
              y,x = yx
              visited.add(yx)
              if yx == (mazeHeight-1,mazeWidth-1):
                  return distance
      
              if y+1 < mazeHeight:
                  if not maze[y+1][x] and (y+1,x) not in visited:
                      queue.append(((y+1,x),distance+1, cost))
      
              if y-1 >= 0:
                  if not maze[y-1][x] and (y-1,x) not in visited:
                      queue.append(((y-1,x),distance+1, cost))
      
              if x+1 < mazeWidth:
                  if not maze[y][x+1] and (y,x+1) not in visited:
                      queue.append(((y,x+1),distance+1, cost))
      
              if x-1 >= 0:
                  if not maze[y][x-1] and (y,x-1) not in visited:
                      queue.append(((y,x-1),distance+1, cost))
      
              if cost == 0:
                  if y+2 < mazeHeight:
                      if not maze[y+2][x] and (y+2,x) not in visited and maze[y+1][x] == 1:
                          queue.append(((y+2,x),distance+2, cost+1))
      
                  if y-1 >= 0:
                      if not maze[y-2][x] and (y-2,x) not in visited  and maze[y-1][x] == 1:
                          queue.append(((y-2,x),distance+2, cost+1))
      
                  if x+1 < mazeWidth:
                      if not maze[y][x+2] and (y,x+2) not in visited and maze[y][x+1] == 1:
                          queue.append(((y,x+2),distance+2, cost+1))
      
                  if x-1 >= 0:
                      if not maze[y][x-2] and (y,x-2) not in visited and maze[y][x-1] == 1:
                          queue.append(((y,x-2),distance+2, cost+1))
      
          return False