2025-10-08 11:08:58 -04:00
|
|
|
"""
|
|
|
|
|
name: Nicholas Tamassia
|
|
|
|
|
|
|
|
|
|
Honor Code and Acknowledgments:
|
|
|
|
|
|
|
|
|
|
This work complies with the JMU Honor Code.
|
|
|
|
|
|
|
|
|
|
Comments here on your code and submission.
|
|
|
|
|
"""
|
|
|
|
|
|
2025-10-10 14:24:50 -04:00
|
|
|
from collections import defaultdict, deque
|
2025-10-08 11:08:58 -04:00
|
|
|
|
|
|
|
|
|
2025-10-10 14:24:50 -04:00
|
|
|
def whatever_first_search(graph: dict[str, set[str]], start: str, stop: str):
|
2025-10-08 11:08:58 -04:00
|
|
|
|
2025-10-10 14:24:50 -04:00
|
|
|
bag: deque[tuple[str | None, str]] = deque()
|
|
|
|
|
bag.append((None, start))
|
|
|
|
|
visited: set[str] = set()
|
|
|
|
|
parent: dict[str, str | None] = {}
|
|
|
|
|
|
|
|
|
|
while len(bag) != 0:
|
|
|
|
|
parent_node, u = bag.pop()
|
|
|
|
|
if u in visited:
|
|
|
|
|
continue
|
|
|
|
|
visited.add(u)
|
|
|
|
|
parent[u] = parent_node
|
|
|
|
|
|
|
|
|
|
if u == stop:
|
|
|
|
|
path: list[str] = []
|
|
|
|
|
while u is not None:
|
|
|
|
|
path.append(u)
|
|
|
|
|
u = parent[u]
|
|
|
|
|
path.reverse()
|
|
|
|
|
return path
|
|
|
|
|
|
|
|
|
|
for v in graph[u]:
|
|
|
|
|
if v not in visited:
|
|
|
|
|
bag.append((u, v))
|
|
|
|
|
|
|
|
|
|
return None
|
2025-10-08 11:08:58 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
# All modules for CS 412 must include a main method that allows it
|
|
|
|
|
# to imported and invoked from other python scripts
|
|
|
|
|
def main():
|
|
|
|
|
n: int = int(input())
|
|
|
|
|
|
2025-10-10 14:24:50 -04:00
|
|
|
graph: dict[str, set[str]] = defaultdict(set)
|
2025-10-08 11:08:58 -04:00
|
|
|
|
|
|
|
|
for _ in range(0, n):
|
|
|
|
|
u, v = input().split()
|
2025-10-10 14:24:50 -04:00
|
|
|
graph[u].add(v)
|
|
|
|
|
graph[v].add(u)
|
2025-10-08 11:08:58 -04:00
|
|
|
|
|
|
|
|
target = input().split()
|
|
|
|
|
|
|
|
|
|
path = whatever_first_search(graph, target[0], target[1])
|
|
|
|
|
|
2025-10-10 14:24:50 -04:00
|
|
|
if path is None:
|
2025-10-08 11:08:58 -04:00
|
|
|
print("no route possible")
|
|
|
|
|
else:
|
|
|
|
|
print(" ".join(path))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
main()
|