Tap here to switch tabs
Problem
Submissions

Problem: Nearest Exit from Entrance in Maze

Try to solve the Nearest Exit from Entrance in Maze problem.

Statement

You are given an m×nm \times n matrix maze with 00 indexed grid positions consisting of empty cells denoted by '.' and walls denoted by '+'. You are also provided an array entrance = [[entrance_row, entrance_col]] indicating the coordinates of the cell where you start.

From any cell, you can move to an adjacent empty cell in one of the four cardinal directions: up, down, left, or right. You cannot enter a cell containing a wall, and you cannot walk outside the boundaries of the grid.

An exit is defined as any empty cell located on the border of maze that is not the entrance itself.

Return the minimum number of steps required to travel from entrance to the closest exit. If no path to an exit exists, return −1-1.

Constraints:

  • maze.length ==== m

  • maze[i].length ==== n

  • 1≤1 \leq m, n ≤100\leq 100

  • maze[i][j] is either '.' or '+'

  • entrance.length ==2== 2

  • 0≤0 \leq entrance_row << m

  • 0≤0 \leq entrance_col << n

  • entrance will always be an empty cell

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Nearest Exit from Entrance in Maze

Try to solve the Nearest Exit from Entrance in Maze problem.

Statement

You are given an m×nm \times n matrix maze with 00 indexed grid positions consisting of empty cells denoted by '.' and walls denoted by '+'. You are also provided an array entrance = [[entrance_row, entrance_col]] indicating the coordinates of the cell where you start.

From any cell, you can move to an adjacent empty cell in one of the four cardinal directions: up, down, left, or right. You cannot enter a cell containing a wall, and you cannot walk outside the boundaries of the grid.

An exit is defined as any empty cell located on the border of maze that is not the entrance itself.

Return the minimum number of steps required to travel from entrance to the closest exit. If no path to an exit exists, return −1-1.

Constraints:

  • maze.length ==== m

  • maze[i].length ==== n

  • 1≤1 \leq m, n ≤100\leq 100

  • maze[i][j] is either '.' or '+'

  • entrance.length ==2== 2

  • 0≤0 \leq entrance_row << m

  • 0≤0 \leq entrance_col << n

  • entrance will always be an empty cell