简体   繁体   中英

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

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)]

In for loop 0 0

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

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

In for loop 0 1

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

endpoint 0 2

In for loop 0 1

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

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

In for loop 1 1

[['x', 'x', 'x'], [1, 'x', 3], [1, 2, 3]] -----> THIS ROW SHOULD NOT CONTAIN x IN POSITION 3 IN THE FIRST ROW

endpoint 1 2

In for loop 1 1

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

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

In for loop 2 1

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

endpoint 2 2

In for loop 2 1

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

[(1, 0)]

In for loop 2 0

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

[]

In for loop 1 1

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

[]

In for loop 0 0

[['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. If I understood your problem correctly

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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