[英]Why is my program running an infinite loop? Python
我正在尝试使用广度优先搜索在 python 中创建一个迷宫求解器。 该算法应该用 1 替换从 S 到 E(开始到结束)的最短路径。 但是,我的程序正在运行一个无限循环,而我唯一的 while 循环在最后,所以我认为问题就在那里。 我无法弄清楚是什么原因造成的。
import queue
def maze_3():
maze3 = [
'00000S000000000000000000000000000000000000000000000000000000000',
'00000 000000000 ',
'00000 000000000 000000000000000000 000000000000000 00000000',
'000000 00000000 000000 00000',
' 00000000 000000000000 000000000000000000 000000',
'00000000 000000000000000000000000000000 0000 000000',
'000000 000 000000000 000 000000 0000',
'000000 000 000000000 00000000 000000 00',
'00 000 0 0000000000 000 000 0000 00 00 00000 000',
'000000 000000 000000000 000 0000 00000',
'0000000000000000 0000000 0000000 000 00000000000',
'000000 000 0000000 0000 00000000000000 00000000',
'0000000000000000E 0000000',
]
return maze3
def finished_maze(maze, solution=''):
global starting_point
for i, position in enumerate(maze[0]):
if position == 'S':
starting_point = i
j = starting_point
k = 0
position = set()
for move in solution:
if move == 'Up':
k -= 1
elif move == 'Down':
k += 1
elif move == 'Left':
j -= 1
elif move == 'Right':
j += 1
for k, row in enumerate(maze):
for j, column in enumerate(row):
if (k, j) in position:
print('1', end='')
else:
print(column, end='')
print()
def is_valid(maze, moves):
a = True
for l, position in enumerate(maze[0]):
if position == 'S':
starting_point = l
j = starting_point
k = 0
for move in moves:
if move == 'Up':
k -= 1
elif move == 'Down':
k += 1
elif move == 'Left':
j -= 1
elif move == 'Right':
j += 1
if not ((j >= 0 and j < len(maze[0])) and (k >= 0 and k < len(maze[0]))):
return not a
elif maze[k][j] == '0':
return not a
return a
def find_solution(maze, moves):
a = True
for i, position in enumerate(maze[0]):
if position == 'S':
starting_point = i
j = starting_point
k = 0
for move in moves:
if move == 'Up':
k -= 1
elif move == 'Down':
k += 1
elif move == 'Left':
j -= 1
elif move == 'Right':
j += 1
if maze[k][j] == 'E':
print(finished_maze(maze, moves))
return a
return not a
space = queue.Queue()
space.put('')
put = ''
maze = maze_3()
while not find_solution(maze, put):
put = space.get()
for i in ['Up', 'Down', 'Left', 'Right']:
location = put + i
if is_valid(maze, location):
space.put(location)
这行代码没有做你期望它做的事情:
for move in moves:
它将它拆分,以便分别为您提供每个字母。
D
o
w
n
D
o
w
n
它应该是这样的:(将import re
添加到文件的开头)
for move in re.findall('[A-Z][^A-Z]*', moves):
它给出了以下内容:
Right
Down
Down
Up
Up
Down
Down
Up
Down
Down
Down
Up
Left
Down
Down
Up
Right
除此之外,您的程序可以工作,但速度很慢,因为您没有检查您是否已经访问过一个节点。
访问列表中的额外部分
如果你像这样定义迷宫:
def maze_3():
maze3 = [
'00000S000000000000000000000000000000000000000000000000000000000',
'00000 000000000 ',
'00000 000000000 000000000000000000 000000000000000 00000000',
'000000E 00000000 000000 00000', # here we add an E
' 00000000 000000000000 000000000000000000 000000',
'00000000 000000000000000000000000000000 0000 000000',
'000000 000 000000000 000 000000 0000',
'000000 000 000000000 00000000 000000 00',
'00 000 0 0000000000 000 000 0000 00 00 00000 000',
'000000 000000 000000000 000 0000 00000',
'0000000000000000 0000000 0000000 000 00000000000',
'000000 000 0000000 0000 00000000000000 00000000',
'0000000000000000E 0000000',
]
return maze3
它提供了以下解决方案,这是正确的:
Down
Down
Right
Down
但是如果我们打印它检查的所有路径,我们可以看到:
Up
Down
Left
Right
DownUp
...
最后一行说它返回到它已经访问过的位置。 如果您更好地了解 BFS,您会发现它保留了访问节点的列表。 如果您添加它,它将加速您的代码,因为您不会检查您已经访问过的节点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.