简体   繁体   English

使用递归在地图上寻找可能的路径。 需要帮助

[英]Finding possible paths on a map using recursion. Help needed

I´ve been trying to solve a problem where I want to be able to go through a grid/map (se below) from the top left corner to the right column.我一直在尝试解决一个问题,我希望能够从左上角到右列通过网格/地图(见下文)。 The idea is to find all possible paths through the grid.这个想法是通过网格找到所有可能的路径。

The grid网格

1 2 3 1 2 3

1 2 3 1 2 3

1 2 3 1 2 3

I have constructed a recursive method that works by finding all possible directions for a specifik position (junction) in the grid (for example one might be able to go south and east) and then go through the possible directions by calling the same recursive method again.我已经构建了一个递归方法,它的工作原理是找到网格中特定位置(连接点)的所有可能方向(例如,一个人可能能够向南和向东),然后通过再次调用相同的递归方法来遍历可能的方向. If we reach the right column the recursion ends and we go back to the previous position and try other direction/s.如果我们到达右列,递归结束,我们回到之前的位置并尝试其他方向。

To avoid loops the method also keeps track of the current path so we avoid going back or in loops.为了避免循环,该方法还跟踪当前路径,因此我们避免返回或循环。 It is this list of current path that Im having problems with.这是我遇到问题的当前路径列表。 When I reach the the first base case (ending the recursive call) and the recursive method tries another direction the list has changed (even though it should not) to contain the path for the base case.当我到达第一个基本情况(结束递归调用)并且递归方法尝试另一个方向时,列表已更改(即使它不应该)包含基本情况的路径。

Looking at the printout below I point out the first error.查看下面的打印输出,我指出了第一个错误。 The list containing current path does for some reason contain the historic path which it should not do.包含当前路径的列表出于某种原因确实包含它不应该做的历史路径。

[(0, 1), (1, 0)] [(0, 1), (1, 0)]

In for loop 0 0在 for 循环 0 0

[['x', 2, 3], [1, 2, 3], [1, 2, 3]] [['x', 2, 3], [1, 2, 3], [1, 2, 3]]

[(0, 2), (1, 1)] [(0, 2), (1, 1)]

In for loop 0 1在 for 循环 0 1

[['x', 'x', 3], [1, 2, 3], [1, 2, 3]] [['x', 'x', 3], [1, 2, 3], [1, 2, 3]]

endpoint 0 2端点 0 2

In for loop 0 1在 for 循环 0 1

[['x', 'x', 'x'], [1, 2, 3], [1, 2, 3]] [['x', 'x', 'x'], [1, 2, 3], [1, 2, 3]]

[(1, 2), (2, 1), (1, 0)] [(1, 2), (2, 1), (1, 0)]

In for loop 1 1在 for 循环 1 1

[['x', 'x', 'x'], [1, 'x', 3], [1, 2, 3]] -----> THIS ROW SHOULD NOT CONTAIN x IN POSITION 3 IN THE FIRST ROW [['x', 'x', 'x'], [1, 'x', 3], [1, 2, 3]] -----> 这行不应该在位置 3 中包含 x第一排

endpoint 1 2端点 1 2

In for loop 1 1在 for 循环 1 1

[['x', 'x', 'x'], [1, 'x', 'x'], [1, 2, 3]] [['x', 'x', 'x'], [1, 'x', 'x'], [1, 2, 3]]

[(2, 2), (2, 0)] [(2, 2), (2, 0)]

In for loop 2 1在 for 循环 2 1

[['x', 'x', 'x'], [1, 'x', 'x'], [1, 'x', 3]] [['x', 'x', 'x'], [1, 'x', 'x'], [1, 'x', 3]]

endpoint 2 2端点 2 2

In for loop 2 1在 for 循环 2 1

[['x', 'x', 'x'], [1, 'x', 'x'], [1, 'x', 'x']] [['x', 'x', 'x'], [1, 'x', 'x'], [1, 'x', 'x']]

[(1, 0)] [(1, 0)]

In for loop 2 0在 for 循环 2 0

[['x', 'x', 'x'], [1, 'x', 'x'], ['x', 'x', 'x']] [['x', 'x', 'x'], [1, 'x', 'x'], ['x', 'x', 'x']]

[] []

In for loop 1 1在 for 循环 1 1

[['x', 'x', 'x'], ['x', 'x', 'x'], ['x', 'x', 'x']] [['x', 'x', 'x'], ['x', 'x', 'x'], ['x', 'x', 'x']]

[] []

In for loop 0 0在 for 循环 0 0

[['x', 'x', 'x'], ['x', 'x', 'x'], ['x', 'x', 'x']] [['x', 'x', 'x'], ['x', 'x', 'x'], ['x', 'x', 'x']]

[] []

Any help is greatly appreciated!!!!!任何帮助是极大的赞赏!!!!!

#This method is called each time a new path is taken
def goNextPath(tmp_mapOfMudCopy,tmp_y,tmp_x,tmp_high):

    # ~ print (tmp_mapOfMudCopy)
    tmp_mapOfMudCopy[tmp_y][tmp_x] = 'x'         #This marks that we have been here and we dont want to go here again


    #Ok first check if we reached an endpoint then just return. This is the base case of the recursion
    if tmp_x == (len(tmp_mapOfMudCopy[tmp_y]) - 1):
        # tmp_mapOfMudCopy[tmp_y][tmp_x] = 'x' #This marks that we have been here and we dont want to go here again
        print('endpoint',tmp_y,tmp_x)
        return



    #This is not an endpoint so fitmp_mapOfMudCopynd which possible directions we can go and add to list. Check so we dont go the same way again
    listPDirections = []
    #Check north. Check also if we have been there before
    if tmp_y > 0: #First check that north is possible
        if tmp_mapOfMudCopy[tmp_y - 1][tmp_x] != 'x': #Now check if we have benn there
            listPDirections.append((tmp_y - 1,tmp_x))
    #Check east.
    if tmp_x < (len(tmp_mapOfMudCopy[tmp_y]) - 1):
        if tmp_mapOfMudCopy[tmp_y][tmp_x + 1] != 'x':
            listPDirections.append((tmp_y,tmp_x + 1))
    #Check south.
    if tmp_y < (len(tmp_mapOfMudCopy) - 1):
        if tmp_mapOfMudCopy[tmp_y + 1][tmp_x] != 'x':
            listPDirections.append((tmp_y + 1,tmp_x))
    #Check west.
    if tmp_x > 0:
        if tmp_mapOfMudCopy[tmp_y][tmp_x - 1] != 'x':
            listPDirections.append((tmp_y,tmp_x - 1))

    print (listPDirections)

    tmp_mapOfMudCopy_path = tmp_mapOfMudCopy.copy() #Copy the map with 'x' marking current path

    for direction in listPDirections:
        print ('In for loop ',tmp_y,tmp_x)
        print(tmp_mapOfMudCopy_path)
        goNextPath(tmp_mapOfMudCopy_path,direction[0],direction[1],tmp_high)



#This method finds the shallowest depth of mud route
def findPath():

    tmp_mapOfMud = [[1,2,3],[1,2,3],[1,2,3]] #Create a map with two rows and three columns
    goNextPath(tmp_mapOfMud,0,0,0) #Call the path finding method and



#Main method this is run when program starts and calls the other methods
if __name__ == '__main__':
    findPath()

Have a look at graph theory ... something like this https://www.geeksforgeeks.org/find-paths-given-source-destination/ should help.看看图论......像这样的东西https://www.geeksforgeeks.org/find-paths-given-source-destination/应该会有所帮助。 If I understood your problem correctly如果我正确理解你的问题

通过使用 deepcopy 解决了这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM