Museum Corridor Sweep
The museum security team is preparing a scheduled sweep to ensure that every hall is checked in a fixed order. Each corridor connects two rooms and allows travel in both directions. The team has mapped out the museum by labelling each room from 0 to n - 1 and recording which corridors connect which rooms. Starting from a designated room, the sweep moves through corridors room by room, spreading the alert to each room visited along the path. The guards want to know how many rooms will be covered if the sweep never revisits a room once it has already been checked.
Write a function that receives the room count n, a list of corridors, and an integer start. Each corridor is represented as [a, b], meaning there is an open, two-way passage between room a and room b. The list may contain duplicates due to maintenance logs, but duplicates should not change the final coverage. Some rooms may have no corridors at all. The sweep begins at start and continues across corridors to unchecked rooms until every reachable room has been checked.
Your output should be the total number of unique rooms covered by the sweep, including start. Once the sweep reaches a room that has already been visited, it stops exploring that branch to avoid double-counting. Disconnected sections of the museum that cannot be reached from start are ignored. If start has no connecting corridor, the sweep covers only that single room. If the entire museum is connected, the function returns n.
Example 1:
Input: n = 5, corridors = [[0,1],[1,2],[2,3],[3,4]], start = 0
Output: 5
Explanation: The sweep moves through every room in sequence without revisiting any room.
Example 2:
Input: n = 6, corridors = [[0,1],[1,2],[0,2],[3,4]], start = 1
Output: 3
Explanation: Rooms 0, 1, and 2 form one connected section; rooms 3 and 4 are separate.
Example 3:
Input: n = 4, corridors = [[0,1],[2,3]], start = 2
Output: 2
Explanation: The sweep reaches rooms 2 and 3; the other rooms remain unchecked.
Related Problems
No related problems found
Comments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this problem.
