Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用队列的Python宽度优先搜索矩阵_Python_Matrix_Path_Breadth First Search_Maze - Fatal编程技术网

使用队列的Python宽度优先搜索矩阵

使用队列的Python宽度优先搜索矩阵,python,matrix,path,breadth-first-search,maze,Python,Matrix,Path,Breadth First Search,Maze,我试图编写一个代码,检查迷宫中从第一个坐标到最后一个由矩阵表示的路径。我还尝试使用队列。以下是我目前掌握的代码: from queue import Queue maze=open(input()) matrix=maze.readlines() matrix=[i.strip() for i in matrix] matrix=[i.split() for i in matrix] q=Queue() row=0 column=0 q.put(row,column) while not q.e

我试图编写一个代码,检查迷宫中从第一个坐标到最后一个由矩阵表示的路径。我还尝试使用队列。以下是我目前掌握的代码:

from queue import Queue
maze=open(input())
matrix=maze.readlines()
matrix=[i.strip() for i in matrix]
matrix=[i.split() for i in matrix]
q=Queue()
row=0
column=0
q.put(row,column)
while not q.empty():
     row,col=q.get()
     if matrix[row][col+1]=="0" and col+1<len(matrix[0]):
         q.put(row,col+1)
         matrix[row][col+1]="2"
    if matrix[row+1][col]=="0" and row+1<len(matrix):
         q.put(row+1,col)
         matrix[row+1][col]="3"
    if matrix[row][col-1]=="0" and col-1>len(matrix[0]):
         q.put(row,col-1)
         matrix[x][x-1]="4"
    if matrix[row-1][col]=="0" and row-1<len(matrix):
         q.put(row-1,col)
         matrix[row-1][col]="5"
我试着在最后加上这个。我运行我的代码,它说“'int'对象不可编辑”


您的代码有几个问题

from queue import Queue

def show(matrix):
    for line in matrix:
        print(*line)
    print()

maze = '''\
0 0 0 0 1 0 0 0
0 1 1 0 1 0 1 0
0 1 0 0 1 0 1 0
0 0 0 1 0 0 1 0
0 1 0 1 0 1 1 0
0 0 1 1 0 1 0 0
1 0 0 0 0 1 1 0
0 0 1 1 1 1 0 0
'''

matrix = maze.splitlines()
matrix = [i.strip() for i in matrix]
matrix = [i.split() for i in matrix]
numrows, numcols = len(matrix), len(matrix[0])

show(matrix)

# Explore the maze
q = Queue()
row = col = 0
q.put((row, col))
while not q.empty():
    row, col = q.get()

    if col+1 < numcols and matrix[row][col+1] == "0":
         q.put((row, col+1))
         matrix[row][col+1] = "2"
    if row+1 < numrows and matrix[row+1][col] == "0":
         q.put((row+1, col))
         matrix[row+1][col] = "3"
    if 0 <= col-1 and matrix[row][col-1] == "0":
         q.put((row, col-1))
         matrix[row][col-1] = "4"
    if 0 <= row-1 and matrix[row-1][col] == "0":
         q.put((row-1, col))
         matrix[row-1][col] = "5"

show(matrix)
row, col = numrows - 1, numcols - 1
current = matrix[row][col]
if current == "0":
    print('No path exists')
else:
    print('Success!')
首先,您需要将(行、列)元组放入队列中

其次,您需要更改` if测试中逻辑的顺序。首先测试新行或列索引是否在矩阵中,然后测试该位置是否包含“0”

映射完整个矩阵后,只需测试矩阵中的最后一个位置是否等于“0”

这是您的代码的修复版本

from queue import Queue

def show(matrix):
    for line in matrix:
        print(*line)
    print()

maze = '''\
0 0 0 0 1 0 0 0
0 1 1 0 1 0 1 0
0 1 0 0 1 0 1 0
0 0 0 1 0 0 1 0
0 1 0 1 0 1 1 0
0 0 1 1 0 1 0 0
1 0 0 0 0 1 1 0
0 0 1 1 1 1 0 0
'''

matrix = maze.splitlines()
matrix = [i.strip() for i in matrix]
matrix = [i.split() for i in matrix]
numrows, numcols = len(matrix), len(matrix[0])

show(matrix)

# Explore the maze
q = Queue()
row = col = 0
q.put((row, col))
while not q.empty():
    row, col = q.get()

    if col+1 < numcols and matrix[row][col+1] == "0":
         q.put((row, col+1))
         matrix[row][col+1] = "2"
    if row+1 < numrows and matrix[row+1][col] == "0":
         q.put((row+1, col))
         matrix[row+1][col] = "3"
    if 0 <= col-1 and matrix[row][col-1] == "0":
         q.put((row, col-1))
         matrix[row][col-1] = "4"
    if 0 <= row-1 and matrix[row-1][col] == "0":
         q.put((row-1, col))
         matrix[row-1][col] = "5"

show(matrix)
row, col = numrows - 1, numcols - 1
current = matrix[row][col]
if current == "0":
    print('No path exists')
else:
    print('Success!')

现在看看你是否能打印出穿过迷宫的路径。
提示:从
矩阵[numrows-1][numcols-1]
开始,然后向后操作


顺便说一句,您可能应该为此任务使用队列,而不是通常在程序使用线程时使用的
队列。

矩阵[x[0]][x[1]+1]==“2”
进行比较并丢弃结果。这应该是
矩阵[x[0]][x[1]+1]=“2”
?如果显示一些典型的迷宫数据,分析代码会容易得多。为了创建一个列表,您可以将矩阵硬编码为代码中的列表。此外,所有这些
x[0]
x[1]
都会使代码难以阅读。如果您将坐标拆分为一对变量(如
row,col=q.get()
),会更容易看到!我编辑了我的代码,谢谢!我不确定最后一个建议是否正确。希望现在更容易阅读。
row
col
都是纯整数,所以如果矩阵[row][col+1]==“0”
非常感谢!我会注意的@I.V.N要了解如何通过迷宫追溯路径,请看
from queue import Queue

def show(matrix):
    for line in matrix:
        print(*line)
    print()

maze = '''\
0 0 0 0 1 0 0 0
0 1 1 0 1 0 1 0
0 1 0 0 1 0 1 0
0 0 0 1 0 0 1 0
0 1 0 1 0 1 1 0
0 0 1 1 0 1 0 0
1 0 0 0 0 1 1 0
0 0 1 1 1 1 0 0
'''

matrix = maze.splitlines()
matrix = [i.strip() for i in matrix]
matrix = [i.split() for i in matrix]
numrows, numcols = len(matrix), len(matrix[0])

show(matrix)

# Explore the maze
q = Queue()
row = col = 0
q.put((row, col))
while not q.empty():
    row, col = q.get()

    if col+1 < numcols and matrix[row][col+1] == "0":
         q.put((row, col+1))
         matrix[row][col+1] = "2"
    if row+1 < numrows and matrix[row+1][col] == "0":
         q.put((row+1, col))
         matrix[row+1][col] = "3"
    if 0 <= col-1 and matrix[row][col-1] == "0":
         q.put((row, col-1))
         matrix[row][col-1] = "4"
    if 0 <= row-1 and matrix[row-1][col] == "0":
         q.put((row-1, col))
         matrix[row-1][col] = "5"

show(matrix)
row, col = numrows - 1, numcols - 1
current = matrix[row][col]
if current == "0":
    print('No path exists')
else:
    print('Success!')
0 0 0 0 1 0 0 0
0 1 1 0 1 0 1 0
0 1 0 0 1 0 1 0
0 0 0 1 0 0 1 0
0 1 0 1 0 1 1 0
0 0 1 1 0 1 0 0
1 0 0 0 0 1 1 0
0 0 1 1 1 1 0 0

4 2 2 2 1 5 2 2
3 1 1 3 1 5 1 3
3 1 4 3 1 5 1 3
3 2 2 1 5 2 1 3
3 1 3 1 5 1 1 3
3 2 1 1 5 1 4 3
1 3 2 2 2 1 1 3
4 3 1 1 1 1 4 3

Success!