-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalgo.py
48 lines (38 loc) · 1.12 KB
/
algo.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
# Define the available tickets as a dictionary with cities as keys and destinations as values.
tickets = {
'Paris': 'Skopje',
'Zurich': 'Amsterdam',
'Prague': 'Zurich',
'Barcelona': 'Berlin',
'Kiev': 'Prague',
'Skopje': 'Paris',
'Amsterdam': 'Barcelona',
'Berlin': 'Kiev',
'Berlin': 'Amsterdam',
}
# Function to find the route using depth-first search (DFS)
def find_route(start_city, tickets):
visited = set()
route = []
def dfs(city):
visited.add(city)
route.append(city)
if city not in tickets:
return True
destination = tickets[city]
if destination not in visited:
if dfs(destination):
return True
route.pop()
visited.remove(city)
return False
if dfs(start_city):
return route
else:
return None
# Start the search from Kiev (as you mentioned your son started from Kiev)
son_route = find_route('Kiev', tickets)
if son_route:
print("Your son's route was:", son_route)
else:
print("No valid route found.")