Advertisement

Nearest Exit from Entrance in Maze - LeetCode 1926 Solution

Nearest Exit from Entrance in Maze - Complete Solution Guide

Nearest Exit from Entrance in Maze is LeetCode problem 1926, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

You are given an m x n matrix maze ( 0-indexed ) with empty cells (represented as '.' ) and walls (represented as '+' ). You are also given the entrance of the maze, where entrance = [entrance row , entrance col ] denotes the row and column of the cell you are initially standing at. In one step, you can move one cell up , down , left , or right . You cannot step into a cell with a wall, and you cannot step outside the maze. Your goal is to find the nearest exit from the entrance . An exit is def

Detailed Explanation

The problem asks us to find the shortest path (in terms of number of steps) from a given entrance cell in a maze to the nearest exit. The maze is represented by a 2D grid where '.' represents an empty cell and '+' represents a wall. An exit is an empty cell located at the boundary of the maze, excluding the entrance itself. We can move one step up, down, left, or right. If no exit is reachable, we should return -1.

Solution Approach

The solution uses BFS to explore the maze. Starting from the entrance cell, it explores all possible paths one step at a time. For each cell visited, it checks if it is an exit (a boundary cell that is not the entrance). If an exit is found, the number of steps taken to reach that exit is returned. If the BFS completes without finding an exit, it means there is no path to an exit, and -1 is returned. The maze is modified to mark visited cells, preventing cycles and redundant exploration.

Step-by-Step Algorithm

  1. Step 1: Initialize a queue with the entrance cell and its initial step count (0). Mark the entrance as visited by changing its value in the maze to '+'.
  2. Step 2: While the queue is not empty, dequeue a cell (row, col) and its step count from the queue.
  3. Step 3: Explore the four possible directions (up, down, left, right) from the current cell.
  4. Step 4: For each direction, check if the neighboring cell is within the maze bounds, is an empty cell ('.'), and has not been visited yet (not '+').
  5. Step 5: If the neighboring cell meets the criteria in Step 4, check if it is an exit (i.e., is on the boundary of the maze). If it is an exit, return the current step count + 1.
  6. Step 6: If the neighboring cell is not an exit but is a valid cell, mark it as visited by changing its value in the maze to '+', and enqueue it into the queue with an incremented step count (steps + 1).
  7. Step 7: If the queue becomes empty before finding any exit, return -1.

Key Insights

  • Insight 1: Breadth-First Search (BFS) is suitable for finding the shortest path in an unweighted graph (maze).
  • Insight 2: We can use the maze itself to keep track of visited cells to avoid cycles by marking visited cells as walls (+).
  • Insight 3: Checking for exit condition within the BFS traversal improves efficiency; we don't need to explore the entire maze if an exit is found early.

Complexity Analysis

Time Complexity: O(m*n)

Space Complexity: O(m*n)

Topics

This problem involves: Array, Breadth-First Search, Matrix.

Companies

Asked at: Samsung, eBay.