1164. Mirror Maze Exits
A light-show room is an m x n grid whose cells are empty (.) or hold a diagonal mirror (/ or \). Beams enter through the border at 2(m + n) numbered ports, clockwise: ports 0..n-1 are the top edge from left to right (beam moves down), the next m are the right edge from top to bottom (moves left), the next n are the bottom edge from right to left (moves up), and the last m are the left edge from bottom to top (moves right).
A / turns a right-moving beam up, an up-moving beam right, a left-moving beam down and a down-moving beam left; a \ turns right to down, down to right, left to up and up to left. A beam always leaves the grid through the port on the edge it crosses. Return exit where exit[p] is the port through which the beam entering at port p leaves. Here 1 <= m, n <= 30.
Tracing every beam takes O((m + n) * m * n) time and O(m + n) space.
Example 1
- Input:
- grid = [["/"]]
- Output:
- [3,2,1,0]
- Explanation:
In a single cell the slash mirror turns every beam by 90 degrees: the top port exits through the left port (3), the right port through the bottom (2), the bottom port through the right (1) and the left port through the top (0).
Example 2
- Input:
- grid = [[".","."],[".","."]]
- Output:
- [5,4,7,6,1,0,3,2]
- Explanation:
With no mirrors every beam crosses straight to the opposite side, so port 0 (top, column 0) leaves through the bottom column 0 port, which is 5.
Constraints
1 ≤ m, n ≤ 30
grid[i][j] is '.', '/' or '\'
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Expected complexity
- Time
- O((m+n)*m*n)
- Space
- O(m+n)