Nearest Exit from Maze Entrance

Given a maze of open cells and walls, return the minimum number of steps required to reach any exit from a given entrance cell. An exit is any open border cell other than the entrance. Pattern focus: BFS shortest path. Expand level by level so the first border cell reached is the answer.

Input Format

maze = character grid, entrance = [row, col]

Output Format

minimum steps to the nearest exit, or -1

Constraints

  • 1 <= rows, cols <= 100
  • Maze contains '.' for open cells and '+' for walls.

Examples

Example 1:

Input:

maze = [["+","+","."],[".",".","."],["+","+","+"]]
entrance = [1,0]

Output:

2

Explanation:

The exit at the right side is the closest reachable border cell.

Example 2:

Input:

maze = [[".","."]]
entrance = [0,0]

Output:

1

Explanation:

The other border cell is a valid exit.

Example 3:

Input:

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

Output:

1

Explanation:

A border exit is one step away.

Loading...
Nearest Exit from Maze Entrance - Shortest Path