You are given a two-dimensional maze represented by an n x m grid. Each cell in the grid can be one of the following:
'.'
: an open space that you can move through.'#'
: a wall that you cannot pass through.'R'
: a rotating obstacle.'S'
: your starting position (there will be exactly one).'E'
: the exit or destination (there will be exactly one).The maze is special in that the rotating obstacles ('R'
) change their state every minute. They alternate between a blocking state (acting as a wall) and a non-blocking state (acting as an open space). Assume that at time 0, all rotating obstacles are in their blocking state. This means at time 1 they become non-blocking, at time 2 they are blocking again, and so on.
You can move to any of the four adjacent cells (up, down, left, right) in one minute. Movement and the toggling of the rotating obstacles’ state happen in discrete minutes. Note that the state change of obstacles occurs between moves (i.e., if you are about to move, consider that the obstacles’ state has just changed to the state corresponding to that minute).
Your task is to write a function or program that finds the minimum number of minutes required to travel from the starting position 'S'
to the exit 'E'
. If there is no possible path, return -1
.
Consider the following while designing your solution:
Develop an efficient solution that finds the shortest time to escape the maze under these constraints.