Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions Module7/practice/02_Maze-2/maze2.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,40 @@
# Скопируйте решение из предыдущей задачи(Maze-1) и адаптируйте под условия текущей задачи
# Чем меньше пришлось вносить изменений в код программы, тем лучше было решение предыдущей задачи
from typing import List, Optional
def bfs(graph: List[List[int]], start_point: int) -> List[Optional[int]]:
"""
Алгоритм поиска в ширину
"""
lengths = [None] * len(graph)
lengths[start_point] = 0
queue = [start_point]
while queue:
cur_vertex = queue.pop(0)
for vertex in graph[cur_vertex]:
if lengths[vertex] is None:
lengths[vertex] = lengths[cur_vertex] + 1
queue.append(vertex)
return lengths

graph = [
[1,3], # 0
[0, 4], # 1
[5], # 2
[0, 4, 6], # 3
[1, 3], # 4
[2, 8], # 5
[3], # 6
[8], # 7
[5, 7], # 8
]

home = 2
bank = 7

path_lengths = bfs(graph, home)
if path_lengths[bank] is not None:
print("Can go to the bank")
else:
print("Can't go to the bank")

# Решите задачу и выведите ответ в нужном формате