A marble sits on an open cell of an R by C maze. Each cell is open (.) or a wall (#). You apply a sequence of tilts, each U, D, L, or R. On a tilt, the marble rolls in that direction, passing over open cells, and does not stop until the next cell in that direction is a wall or lies outside the maze; it then rests on the last open cell it reached. A tilt where the marble cannot move at all leaves it in place. Row 0 is the top, column 0 is the left.
Line 1: two integers R and C.
Next R lines: each a string of C characters, . or #.
Next line: two integers r0 c0, the starting open cell.
Next line: a non-empty string of tilts over U, D, L, R.
A single line r c: the marble's final resting cell.
Example 1
Input
3 3 ... .#. ... 0 0 DR
Expected
2 2
Explanation
Tilting down rolls the marble to (2,0); tilting right rolls it to (2,2).
Example 2
Input
3 3 ... .#. ... 1 0 R
Expected
1 0
Explanation
The wall at (1,1) is immediately to the right, so the marble cannot move and stays at (1,0).
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →