← 返回 sofi 的题目列表Interactive Story Endings Reachability (Choices/Options)
类型:online_judge
You are given a simplified interactive-story model:
choices: a list of choice records. Each record has the form choice = [start, op1, op2], meaning that from node start you can pick one of two options: op1 or op2.
options: a mapping describing where each option op leads (i.e., the next node).
endingList: a list of ending node IDs.
Implement a function that determines which ending nodes in endingList are reachable from a given startNode, assuming the player can repeatedly pick either option at each step.
Requirements:
Reachability may require multiple hops.
Cycles may exist; avoid infinite loops.
During the interview, clearly state your chosen input representation (e.g., options as a dict op -> nextNode) and provide a runnable implementation.
Constraints (you may state reasonable ones): e.g., |choices|, |options| <= 2e5.
Example (illustrative):
choices: [[1, "A", "B"], [2, "C", "D"], ...]
options: { "A": 2, "B": 3, "C": 4, "D": 5 }
endingList: [4, 5]
startNode: 1
Output: [4, 5].
Example
Input
choices=[[1,"A","B"],[2,"C","D"],[3,"E","F"]]
options={"A":2,"B":3,"C":4,"D":5,"E":6,"F":7}
endingList=[4,5,7]
startNode=1
Output
[4,5,7] (order can vary)