python - 同时搜索两个双端队列中的节点并打印队列中的值 - Python 3
问题描述
我有一个广度优先搜索算法的代码。
根据算法的最后 3 行,我们必须在执行任何进一步操作之前检查两个队列中的子状态是否已经可用。在我的代码中,我必须使用 BFS 算法在迷宫中搜索从起点到终点的路径。我创建了一个节点,用于保存网格位置的状态、动作、路径和父节点。代码如下:
#class to initialize node
class Node:
state = []
actions = []
parent = []
path = 0
def __init__(self, state, actions, parent, path):
self.state = state
self.actions = actions
self.parent = parent
self.path = path
在算法函数中,我将节点从起点添加到 Python 中的双端队列。但在添加之前,我必须检查 child.state 不应处于前沿或探索队列的条件。我在代码中的条件无限运行。如下:
def bfs_search(maze_size, start_point, end_point, grid_values, number_of_grids):
#code for creating first node and adding to queue
while True:
#here we have the code to break the loop if queue gets empty and then finding the child
pointer and creating child notes
#this condition does not execute as it should
if child_node.state not in frontier and child_node.state not in explored:
if child_node.state == end_point:
goal = True
explored.appendleft(child_node)
break
else:
frontier.appendleft(child_node)
我还想在测试代码以检查节点是否被附加到队列时打印附加到队列的元素。
解决方案
推荐阅读
- angular - 想要使用任何人都知道的本机脚本创建刮刮卡视图
- php - Yii2 贝宝支付集成
- c# - 在 C# 中从文件流中反序列化多个对象
- android - 如何根据前景图像在android中动态更改状态栏的颜色?
- php - Laravel hasMany 关系选择特定列问题
- python-3.x - 调用服务器后如何恢复调用功能?
- asp.net - 如何将图像路径添加到数据库+将图像与相册关联?
- javascript - JavaScript 三元运算符表达式中的返回语句
- twitter-bootstrap-3 - Bootstrap3嵌套列冲突
- python - 删除python数据框中包含空格的行