-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path689B.py
More file actions
30 lines (22 loc) · 848 Bytes
/
689B.py
File metadata and controls
30 lines (22 loc) · 848 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# @author Matheus Alves dos Santos
n_intersections = int(input())
shortcuts = list(map(int, input().split()))
shortcuts = [(shortcut - 1) for shortcut in shortcuts]
costs = [0] + ([-1] * (n_intersections - 1))
i, queue = 0, [0]
while (i < len(queue)):
intersection = queue[i]
shortcut = shortcuts[intersection]
if costs[shortcut] == -1:
costs[shortcut] = costs[intersection] + 1
queue.append(shortcut)
if intersection < (n_intersections - 1):
if costs[intersection + 1] == -1:
costs[intersection + 1] = costs[intersection] + 1
queue.append(intersection + 1)
if intersection > 0:
if costs[intersection - 1] == -1:
costs[intersection - 1] = costs[intersection] + 1
queue.append(intersection - 1)
i += 1
print(' '.join(map(str, costs)))