python maze

 

# - * - Coding: UTF- . 8 - * - from Collections # Import introduced into the deque queue 
Maze = [ 
    [ . 1 , . 1 , . 1 , . 1 , . 1 , . 1 , . 1 , . 1 , . 1 , . 1 ], 
    [ . 1 , 0 , 0 , 1 , 0 , 0 , 0 , 1 , 0 , 1 ], 
    [ 1 , 0 ,
 

 0,1,0,0,0,1,0,1],
    [1,0,0,0,0,1,1,0,0,1],
    [1,0,1,1,1,0,0,0,0,1],
    [1,0,0,0,1,0,0,0,0,1],
    [1,0,1,0,0,0,1,0,0,1],
    [1,0,1,1,1,0,1,1,0,1], 
    [ 1 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 ], 
    [ 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 ] 
 ] 
 
# four mobile direction 
dirs = [ 
    the lambda X, Y: (X + . 1 , Y), # lower 
    the lambda X, Y: (X - . 1, Y), the # 
    the lambda X, Y: (X, Y - . 1 ), # left 
    the lambda X, Y: (X, Y + . 1 ) # Right 
] 
 
 
DEF print_r (path): 
    "" " printing passes " "" 
    curNode = path [- . 1 ] # last node 
    the realpath = [] # out of the path of
     the while curNode [ 2 ] = -! . 1 : the last node # determines whether the flag is - . 1 , if the explanation is the starting point is -1, If No - . 1 continues to find 
        realpath.append (curNode [ 0 : 2 ]) and adds the nodes to get # x, y coordinate information 
        curNode = path [curNode [ 2]] Here # curNode [ 2 ] is the previous step identifies the node of the current node: path subscripts, so path [curNode [ 2 ]] to get the preceding node 
 
    realpath.append (curNode [ 0 : 2 ]) where # curNode [ 2 ] == - . 1 , is the starting point of the maze, the coordinate information is added to the path 
 
    realpath.reverse () # reverse list, after the previously generated list becomes forward from front to back 
    Print (the realpath) 
 
 
DEF maze_path_queue ( x1, y1, x2, y2) : # (x1, y1) represents the starting point; (x2, y2) on behalf of the end 
    . "" " achieve maze problem with the queue - depth-first search ." "" 
    queue = deque () # create a queue 
    queue .append ((X1, Y1, - . 1 )) # were added starting point, the third parameter is the time of recording it to the Who, where the starting point is set to - . 1 
    path = [] # saved dequeue node
     whilelen (Queue)> 0 : # only team not empty has been circulating 
        curNode = queue.pop () # node out of the first team squad, the coexistence of the current node variable 
        path.append (curNode) # Add to the list of path 
        IF curNode [ 0 ] X2 and curNode == [ . 1 ] == Y2: # determines whether or not go to the end 
            print_r (path) # If reach the end, the print path 
            return True 
 
        for the dir in dirs: # four search directions 
            nextNode = the dir (curNode [ 0 ], curNode [ . 1 ]) # curNode [ 0 ], curNode [ . 1 ] are respectively the current node X, Y
             IF Maze [nextNode [ 0 ]] [nextNode [ . 1]] == 0 : # way road if 
                queue.append ((nextNode [ 0 ], nextNode [ . 1 ], len (path) - . 1 )) # successor node into the team, who let it to the tag: path final index an element 
                Maze [nextNode [ 0 ]] [nextNode [ . 1 ]] = 2    # is set to 2, labeled has gone through 
 
    the else : # If the queue is empty (the current node to the dead end, node deletes no new node is added) , there is no way 
        Print ( " no path " )
         return False 
 
 
maze_path_queue ( . 1 , . 1 , . 8 , . 8 )

 

Guess you like

Origin www.cnblogs.com/sea-stream/p/11100396.html