Rotating maze zone search
InterviewTime limit1sMemory limit1024 MB
Find the fewest hours to reach the exit in a 4k by 4k maze where each hour you move or stay, then the 4x4 zone you stand in rotates 90 degrees clockwise.
- Level
Medium5 of 10
- Topics
- BFS, Graph, Simulation
- Solved
- No attempts yet
Problem
The maze is × in size. Let the top-left cell be and the bottom-right cell be .
The region forms one zone. Put simply, each 4×4 block is a zone. The figure below shows a maze with split into 4×4 zones, each colored differently. The number written at is the zone number.

For example, and are both in zone 1, so they are in the same zone. and are in zones 1 and 2, so they are in different zones.
Each hour, the following steps happen in order until the maze is escaped.
- Move up, down, left, or right, or stay in place. A move is blocked if the target cell is a wall or lies outside the maze.
- After step 1, the zone containing the current position rotates 90 degrees clockwise.
- Every zone except the one containing the current position returns to its initial orientation.
- If the current position is an exit, you escape the maze.
The maze consists of a start cell 'S', open cells '.', walls '#', and exit cells 'E'. Find the minimum time to go from 'S' to 'E'.
Input
The first line contains . Each of the next lines contains characters describing the maze.
Output
Print the minimum time to go from 'S' to 'E'. If escape is impossible, print -1.