Fastest Thing Alive
InterviewTime limit2sMemory limit2048 MB
Find a route from a blank cell in the first row to a blank cell in the last row, moving down one row per step and shifting at most one lane left or right.
- Level
Medium4 of 10
- Topics
- Dynamic programming, Matrix
- Solved
- No attempts yet
Problem
The villainous Doctor Pearman (some prefer to call him Dr. Mechanic) is up to no good again. With his newest machine, he has cracked the surface of the earth to tap into the ancient power hidden below and use it to rule the world. After seeing this, the hero Shonic sets out at once along Dragon Road to reach the Doctor and stop his plan.
Along the road, Shonic comes across a spikefield of rows divided into lanes. Each space in the spikefield is either a spike trap or blank. Shonic has no magic rings left, so one touch of a spike kills him and the Doctor's plan succeeds. He must pass through a blank field in every row. He moves very fast, so he can make only one horizontal move when he goes to the next row. He can go left and move down one row, go right and move down one row, or stay in the same lane and move down one row. He cannot leave the spikefield, so he cannot move left from the leftmost lane. When he enters the spikefield, he can choose the starting field in the first row, but that field must be blank. A successful route takes him through every row, starting in the first row and ending on a blank field in the last row. Help Shonic find a way to the end of the spikefield.
Input
The first line contains two integers () and (), the number of rows and the number of columns in the spikefield.
The next lines each contain a string of length that describes the spikefield. A * (asterisk) marks a spike trap. A _ (underscore) marks a blank field that Shonic can cross.
Output
Print either of the following.
A single integer (), the lane where Shonic starts.
A string of characters, each L, R, or F, for left, right, and forward. This is the sequence of moves that takes Shonic to the end of the spikefield without touching a spike.
If no valid path exists, print impossible.
If several solutions exist, print any one of them.