Skip to content
Decoder

Nearest Exit from Entrance in Maze

Medium

Problem

A maze is a grid where "+" is a wall and "." is open floor. You stand at entrance = [row, col] (an open cell) and can step up, down, left or right onto open floor. An exit is any open cell on the border of the maze, except the entrance itself. Return the fewest steps to reach an exit, or -1 if there is none you can reach.

Example 1

maze = [["+", "+", ".", "+"],

Example 2

        [".", ".", ".", "+"],

Example 3

Input
["+", "+", "+", "."]], entrance = [1, 2]
Output
1
Why
step up to [0, 2]

Example 4

Input
maze = [[".", "+"]], entrance = [0, 0]
Output
-1
Why
the entrance does not count

Constraints

  • 1 <= maze.length, maze[i].length <= 100
  • maze[i][j] is "+" or "."
  • entrance is an open cell inside the maze

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.