UndergroundVault
SRM 171 · 2003-11-12 · by Running Wild
Problem Statement
You begin (and must end) in room 0. When you seal a room, you close all doors in that room that must be sealed from that side. If there are other doors that go to that room that must be sealed from the other side, they remain open (but the room is still considered sealed). Given a
Each element of rooms will be a comma delimited list of numbers. The i-th element of rooms will list all rooms adjacent to room i that must be sealed from room i. For any two rooms i and j, there will be at most one door connecting them. If there is a door connecting them, i will be listed as adjacent to j, or vice versa, but not both.
Notes
- There can be cycles. For example: room i has a door to room j that must be sealed from room i, room j has a door to room k that must be sealed from room j, and room k has a door to room i that must be sealed from room k.
Constraints
- rooms will contain between 1 and 50 elements, inclusive.
- Each element of rooms will contain only the digits '0'-'9' and commas.
- Each element of rooms will only contain values between 0 and the number of elements in rooms - 1, inclusive, and no value will have leading zeros.
- No element of rooms will contain leading or trailing commas, or more than one commma between values.
- Element i of rooms will not contain the value i.
- No element of rooms will contain the same value more than once.
- If element i of rooms contains j, element j of rooms will not contain i.
- There will be a way to seal all the rooms and end in room 0.
Statement by TopCoder, Inc. — view the original on the archive.
{"1","2",""}
Returns: { 2, 1, 0 }
We can't seal room 0 first, because then we won't be able to reach any other rooms. We can't seal room 1 first either because then we can't reach room 2 to seal it. The only way is to seal room 2, then 1, then finally 0.
{"1","2","3","1"}
Returns: { 3, 2, 1, 0 }
Rooms 1, 2, and 3 form a cycle. Each one must seal a door to the next one in the cycle. The only way to seal all the rooms is to go to room 3, seal it, and then work backwards through the cycle.
{"3,5","2","8","","","6,7","","1,8","4"}
Returns: { 2, 1, 3, 4, 6, 8, 7, 5, 0 }
{"1,2,3","4,5,6","5,6,8","8","5,6","7,9","11","3","11","7,10","4",""}
Returns: { 1, 3, 4, 6, 7, 10, 9, 5, 11, 8, 2, 0 }
{"3","2","0","1"}
Returns: { 2, 1, 3, 0 }
Submissions are judged against all 54 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class UndergroundVault with a public method vector<int> sealOrder(vector<string> rooms) · 54 test cases · 2 s / 256 MB per case