查找路径(具有互换的火车路径)的算法

时间:2018-11-15 15:39:05

标签: python python-3.x path-finding

所以这是我在python中简化的代码,我试图使用递归来查找用户想要从station1转到station12的位置(例如),但是我找不到它出来,请帮助我,我坚持了这么长时间,非常感谢!

x = ["station1","station2","station3","station4"]
y = ["station5","station6","station7","station8"]
z = ["station9", "station10"]
e = ["station11", "station12"]
array = x, y, z, e
interchange = [ ["station4", "station5"] , ["station8", "station9"], ["station10, station[11] ]
cur = "station1"
des = "station12"

所以下面的代码是我试图找到的算法,但它根本不起作用。

def find(cur, des):
    check = 0
    for each in array:
        if cur in each and des in each:
            check = 1
            ind = each.index(cur)
            ind2 = each.index(des)
            for i in range(ind, ind2+1):
                print("-->", end = " ")
                print(each[i], end = " ")
            print("\n")
    for each in array:
        if cur in each and check == 0:
            for station in interchange:
                if station in each and cur != station:
                    ind = each.index(cur)
                    ind2 = each.index(station)
                    for i in range(ind, ind2+1):
                        print("-->", end = " ")
                        print(each[i], end = " ")
                    print("\n")
                    find(station, des)

这是我想要得到的结果: station1-> station2-> station3-> station4-> station5-> station6-> station7-> station8-> station9-> station10-> station11-> station12

编辑: Canh 的答案:

def find(cur, des):
    check = 0
    for each in array:
        if cur in each and des in each:
            check = 1
            ind = each.index(cur)
            ind2 = each.index(des)
            for i in range(ind, ind2+1):
                print("-->", end = " ")
                print(each[i], end = " ")
            print("\n")
    for each in array:
        if cur in each and check == 0:
            for station1, station2 in interchange:
                if station1 in each and cur != station1:
                    ind = each.index(cur)
                    ind2 = each.index(station1)
                    for i in range(ind, ind2+1):
                        print("-->", end = " ")
                        print(each[i], end = " ")
                    find(station2, des)

对不起,但我还有另一个问题,如果条件是:

,我该如何修改代码,以便找到所有可能的路线(注意:火车可以前进和后退)。

x = ["station1","station2","station3","station4"]
y = ["station8","station7","station6","station5"]
z = ["station9", "station10"]
e = ["station11", "station12"]

interchange = [ ["station1", "station9"], ["station5", "station4"] , ["station9", "station8"], ["station10", "station11" ],  ["station2", "station9" ] ]

cur = "station12"
des = "station1"

一些路线示例应为:

station12-> station11-> station10-> station9-> station1

station12-> station11-> station10-> station9-> station2-> station1

站12->站11->站10->站8->站7->站6->站5->站4->站3->站2->站1

并且如果cur和des被交换了:

cur = "station1"
des = "station12"

那么可能的路线将与前一条相反。

1 个答案:

答案 0 :(得分:0)

您的interchange变量是列表的列表。在语句for station in interchange中,station是一个列表,因此条件station in each永远不会为True。

我认为应该是这样

for station1, station2 in interchange:
    if station1 in each and cur != station1:
        ind = each.index(cur)
        ind2 = each.index(station1)
        for i in range(ind, ind2+1):
            print("-->", end = " ")
            print(each[i], end = " ")
        print("\n")
        find(station2, des)