Bob moves through a multi-graph where each letter opens doors with that label; given the letter sequence, compute the probability he reaches room n, choosing uniformly among available matching doors.
Medium5ProbabilityDynamic programmingGraphSimulationInterviewNo attempts yetTime limit2sMemory limit512 MBBob is the captain of the USS Spacey McSpaceface, the pride of the Human Empire's space fleet. He is also the last person alive on board.
An unrecognized virus drove the ship's AI, Alpha 5, out of control. Alpha 5 pumped neurotoxin into the crew quarters and killed everyone. Bob was on the bridge at the time, where he grabbed a detoxification kit and a gas mask. Alpha 5 then locked the ship's controls and started the self destruction sequence, so Bob is adrift in space, alone with a rampant AI.
An engineer once told Bob that an override switch sits near the fusion reactor that powers the ship, so Bob starts down toward the engine quarters. The virus has taken over the automatic door system too, and every door now opens and closes on its own.
Bob knows three things about the doors:
Bob wants to keep moving, so he uses a door whenever he can. For each announced letter he walks through a door carrying that letter if such a door leads out of the room he is in. He does not know the way to the core, so when several such doors are available he picks one of them with equal probability. Doors between the same pair of rooms count separately: if three doors carrying an A lead from his room to the same neighbor, then three of his choices lead to that neighbor. If no door out of his room carries the announced letter, Bob waits where he is. As soon as Bob enters room n he flips the override switch and stops moving.
Given the sequence of letters until the explosion, compute the probability that Bob reaches the reactor core in time.
The input consists of:
After the last letter every door slams shut, and the ship explodes if Bob has not reached the switch yet. Bob starts on the bridge, room 1, and the override switch is in room n.
Print the probability, as a percentage, that Bob reaches the override switch in time. Round the value to exactly six digits after the decimal point.