[英]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.