1+ start = [0 ,0 ]
2+ end = [7 ,7 ]
3+ taken = [
4+ [1 ,0 ], [1 ,1 ], [1 ,2 ], [1 ,3 ]
5+ ]
6+ queue = []
7+ queue .append ([start [0 ],start [1 ],- 1 ])
8+ visited = []
9+ maze = []
10+ for i in range (8 ):
11+ maze .append (['.' ,'.' ,'.' ,'.' ,'.' ,'.' ,'.' ,'.' ])
12+ visited .append ([0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ])
13+ maze [start [0 ]][start [1 ]] = 'S'
14+ maze [end [0 ]][end [1 ]] = 'E'
15+ for i in taken :
16+ maze [i [0 ]][i [1 ]] = 'X'
17+ while (len (queue ) > 0 ):
18+ point = queue .pop (0 )
19+ if end [0 ] == point [0 ] and end [1 ] == point [1 ]:
20+ print (point [2 ] + 1 )
21+ break
22+ current = point [2 ] + 1
23+ if point not in taken and visited [point [0 ]][point [1 ]]== 0 :
24+ visited [point [0 ]][point [1 ]] = current
25+ for i in range (point [0 ], - 1 , - 1 ):
26+ if [i ,point [1 ]] in taken :
27+ break
28+ if visited [i ][point [1 ]]== 0 :
29+ queue .append ([i ,point [1 ],current ])
30+ for i in range (point [0 ], 8 ):
31+ if [i ,point [1 ]] in taken :
32+ break
33+ if visited [i ][point [1 ]]== 0 :
34+ queue .append ([i ,point [1 ],current ])
35+ for i in range (point [1 ], - 1 , - 1 ):
36+ if [point [0 ],i ] in taken :
37+ break
38+ if visited [point [0 ]][i ]== 0 :
39+ queue .append ([point [0 ],i ,current ])
40+ for i in range (point [1 ], 8 ):
41+ if [point [0 ],i ] in taken :
42+ break
43+ if visited [point [0 ]][i ]== 0 :
44+ queue .append ([point [0 ],i ,current ])
45+
46+ for i in maze :
47+ for j in i :
48+ print (j , end = ' ' )
49+ print ()
0 commit comments