add visited array

This commit is contained in:
onyx-and-iris 2024-01-11 18:47:35 +00:00
parent ecf623c2c4
commit 10914ee208

View File

@ -21,6 +21,8 @@ graph = {
"f": [], "f": [],
} }
visited = []
def bfs(): def bfs():
queue = deque() queue = deque()
@ -32,6 +34,10 @@ def bfs():
if current_node.name == "f": if current_node.name == "f":
return current_node.distance return current_node.distance
if current_node.name in visited:
continue
visited.append(current_node.name)
next_nodes = graph[current_node.name] next_nodes = graph[current_node.name]
for node in next_nodes: for node in next_nodes:
node.distance = current_node.distance + 1 node.distance = current_node.distance + 1