-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathq4.py
36 lines (26 loc) · 781 Bytes
/
q4.py
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
31
32
33
34
35
36
"""
"""
def solve(end_port, paths):
step = 0
positions = {1: []}
while end_port not in positions:
next_positions = {}
for path in paths:
start_pos = path[step % len(path)]
if start_pos in positions:
next_pos = path[(step + 1) % len(path)]
next_positions[next_pos] = positions[start_pos] + [next_pos]
step += 1
positions = next_positions
return positions[end_port][:-1]
def main():
end_port = int(input())
num_planes = int(input())
paths = []
for _ in range(num_planes):
path = [int(x) for x in input().split(" ")]
paths.append(path)
for step in solve(end_port, paths):
print(step)
if __name__ == "__main__":
main()