如何在Python中找到两个数字坐标之间的短路径?

问题描述 投票:-1回答:1

我正在尝试在二维数组中找到从一个坐标到另一个坐标的最短路径。理想情况下,输出将是从第一坐标到第二坐标必须经过的坐标列表。除此之外,其目的不是要沿对角线移动(例如,只能向上,向下,向左和向右遍历图形)。

完整示例:

arr = [
          [0, 1, 2]
          [3, 4, 5]
          [6, 7, 8]
      ]

coor1 = (0, 2) # seen as 2 in the arr array
coor2 = (2, 1) # seen as 7 in the arr array

def find_shortest_path(firstcoor, secondcoor):
    ''' finds shortest path between two coordinates, can't go diagonally '''
    # no idea how to find the shortest path
    return shortest_path # a list of coordinates to go through

# in this case the shortest path could be e.g. [(0, 2), (1, 2), (2, 2), (2, 1)]

如何计算这两个或任何两个坐标之间的最短路径?

python coordinates path-finding
1个回答
0
投票

没有进一步了解您的问题,

简单的减法运算是否不会为您提供将您从一个位置运送到另一位置的向量?我认为在简单的情况下,这将是最短的向量。

© www.soinside.com 2019 - 2024. All rights reserved.