Home > other >  Python maze, showing all route, including dead ends with arrows showing all the right route
Python maze, showing all route, including dead ends with arrows showing all the right route

Time:11-25

directly on code:

 
# define maze, 1 is the pathway, 0 for the wall, the Internet is a problem with the algorithm, and can only output one
# after I adapted, the algorithm can output all situation and feasible method, and the output process and each kind of method
# this algorithm and the judgment depressions watershed algorithm, which is based on the algorithm of writing
Maze=[[1, 0, 1, 0, 0, 1, 1, 0, 1, 1],
[1, 1, 1, 1, 1, 0, 1, 0, 0, 1],
[0, 0, 1, 0, 1, 0, 1, 0, 0, 1],
[0, 1, 1, 1, 1, 0, 1, 0, 0, 1],
[0, 0, 1, 0, 1, 0, 1, 0, 1, 1],
(1, 0, 0, 1, 1, 1, 1, 1, 0, 1],
[1, 1, 1, 1, 1, 0, 1, 0, 0, 1],
[1, 1, 0, 0, 1, 0, 1, 1, 1, 1],
(1, 0, 1, 0, 1, 0, 1, 0, 1, 1],
(1, 0, 1, 0, 1, 0, 1, 1, 0, 1]]


# judgment coordinates effectiveness, if beyond the array bounds or not satisfy the conditions of the value of 1, explain that point invalid returns False, otherwise it returns True,
Def check (x, y) :
If y & gt;=0 and y & lt; Len (maze) and x & gt; X=0 and & lt; Len (maze [0]) and maze [y] [x]==1:
Return True
The else:
Return False


Def walk (x, y) : # mazes algorithm
Global success, procedure_list success_count # declare these variables with global

Maze [y] [x]=0 # has come need to tag, because can't walk, or beat around the bush
Procedure_list. Append ((x, y)) # process list to join this point coordinates
If x==0 and y==9: # if destination (9, 0)
Print (" success ") # output "successful"
Success=1 # said can go through
Success_count +=1 # successful method by the increase in the number 1
Success_procedure_ls. Append (a list (procedure_list) # save success path
Procedure_list. Remove ((x, y))
Maze [y] [x]=1 # set as path
Return True
If maze [y] [x]==0:
First print (" the first {}, {} column ". The format (y + 1, x + 1))
For pos_y pos_x pos_ls in:
If the check (x + pos_x, y + pos_y) : break
The else:
Print (" dead ends ")
If success & lt; 100:

For pos_y pos_x pos_ls in:

If the check (x + pos_x, y + pos_y) :
Walk (x + pos_x, y + pos_y)

Maze [y] [x]=1
Procedure_list. Remove ((x, y))

Return success


Procedure_list=[]
Success=0 # success
Success_procedure_ls=[] # success method list
Success_count=0 # success method, the initial 0
Pos_ls=[(0, 1), (1, 0), (0, 1), (1, 0)] # respectively, left, down, right
Walk (0, 9) # from (0, 9)
# print (procedure_list)


# for way in success_procedure_ls:
# print (way)
# maze_copy=list (as) a maze of shallow copy (the outermost layer is a deep copy)

X, y=0, 1 # x represents the abscissa, y represents the ordinate
N_way=0
For way in success_procedure_ls:
I=0
N_way +=1
Print (" the first {} ways: ". The format (n_way))
Print (way) # output method
For the line in maze_copy:
As [I]=list (maze_copy [I]) # a maze of deep copy
I +=1 # I point to the next line
For the index in the range (len (way) - 1) : # to convert every step into arrow
If way [index + 1] [x]==way [index] [x] 1:
Maze [way [index] [y]] [way [index] [x]]="please", "
Elif way [index + 1] [x]==way [index] [x] + 1:
Maze [way [index] [1]] [way [index] [0]]="->"
Elif way [index + 1] [y]==way [index] [y] 1:
Maze [way [index] [y]] [way [index] [x]]="write"
Elif way [index + 1] [y]==way [index] [y] + 1:
Maze [way [index] [1]] [way [index] [0]]="left"
For the row in maze:
For the item in a row:
If the item not in [0, 1] :
Print (item, end=')
The else:
Print (STR (item) + "', end=' ')
Print ()
Print (' * '* 100) # each method separately
Print (" {} in the total method ". The format (success_count) # output method for


run results:
 
D: \ Users \ JJH \ AppData \ Local \ designed \ Python \ Python35 \ Python exe D:/Users/JJH/PycharmProjects/untitled/maze_new py
Line 10, column 1
Line 9, column 1
Line 8, column 1
Line 7, column 1
Line 6, column 1
Ends,
Line 7, column 2
Line 7, column 3
Line 7, column 4
Line 6, column 4
Line 6, 5 column
Line 5, 5 column
Line 4, 5 column
Line 4, column 4
Line 4, the third column
Line 4, the second column
Ends,
Line 3, column 3
Line 2, and 3 columns
Line 2, the second column
Line 2, column 1
Line 1, column 1
Ends,
Line 1, column 3
Ends,
Line 2, column 4
Line 2, 5 column
Line 3, column 5
Ends,
Line 5, column 3
Ends,
Line 3, column 5
Line 2, 5 column
Line 2, column 4
Line 2, and 3 columns
Line 2, the second column
Line 2, column 1
Line 1, column 1
Ends,
Line 1, column 3
Ends,
Line 3, column 3
Line 4, the third column
Line 4, the second column
Ends,
Line 4, column 4
Ends,
Line 5, column 3
Ends,
Line 6, 6 columns
Line 6, 7
Line 5, 7
Line 4, 7
Line 3 and 7
Line 2 and 7
Line 1, column 7
Line 1, column 6
Ends,
Line 6, 8 column
Ends,
Line 7, 7
Line 8 and 7
Line 8, the 8th column
Line 8, 9 columns
Line 8, 10 column
Line 7, 10 column
Line 6, column 10
Line 5, 10 column
Line 5, 9 columns
Ends,
Line 4, column 10
Line 3, column 10
Line 2, column 10
Success
Line 9, 10 column
Line 9, 9 columns
Ends,
Line 10, column 10
Ends,
Line 9, 9 columns
Line 9, 10 column
Line 8, 10 column
Line 7, 10 column
Line 6, column 10
Line 5, 10 column
Line 5, 9 columns
Ends,
Line 4, column 10
Line 3, column 10
Line 2, column 10
Success
Line 10, column 10
Ends,
Line 9, 7
Line 10, 7
Line 10, 8 column
Ends,
Line 7, 5 column
Line 8, 5 column
Line 9, 5 column
Line 10, 5 column
Ends,
Line 7, 5 column
Line 6, 5 column
Line 6, column 4
Ends,
Line 5, 5 column
Line 4, 5 column
Line 4, column 4
Line 4, the third column
Line 4, the second column
Ends,
Line 3, column 3
Line 2, and 3 columns
Line 2, the second column
Line 2, column 1
Line 1, column 1
Ends,
Line 1, column 3
Ends,
Line 2, column 4
Line 2, 5 column
Line 3, column 5
Ends,
Line 5, column 3
Ends,
Line 3, column 5
Line 2, 5 column
Line 2, column 4
Line 2, and 3 columns
Line 2, the second column
Line 2, column 1
Line 1, column 1
Ends,
Line 1, column 3
Ends,
Line 3, column 3
Line 4, the third column
Line 4, the second column
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related