-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTornToPieces.py
52 lines (45 loc) · 1.27 KB
/
TornToPieces.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# Torn to pieces
num=int(input())
graph=dict()
for i in range(num):
connection=input().split(" ")
start=connection[0]
for stop in connection[1:]:
if start in graph:
graph[start].append(stop)
else:
graph[start]=[]
graph[start].append(stop)
if stop in graph:
if not start in graph[stop]:
graph[stop].append(start)
else:
graph[stop]=[]
graph[stop].append(start)
def search(start, end):
stack=[]
visited=[]
path=[]
stack.append([start])
while len(stack)>0:
elem=stack.pop(0)
value=elem[-1]
visited.append(value)
if value==end:
return elem
else:
if value in graph:
neighbors=graph[value]
for neighbor in neighbors:
if neighbor not in visited:
toAdd=elem.copy()
toAdd.append(neighbor)
stack.append(toAdd)
toFind=input().split(" ")
path=None
if toFind and len(toFind)==2:
path=search(toFind[0], toFind[1])
if path==None:
print("no route found")
else:
print(*path)