Advertisement

Shortest Path in Binary Matrix - LeetCode 1091 Solution

Shortest Path in Binary Matrix - Complete Solution Guide

Shortest Path in Binary Matrix is LeetCode problem 1091, 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

Given an n x n binary matrix grid , return the length of the shortest clear path in the matrix . If there is no clear path, return -1 . A clear path in a binary matrix is a path from the top-left cell (i.e., (0, 0) ) to the bottom-right cell (i.e., (n - 1, n - 1) ) such that: All the visited cells of the path are 0 . All the adjacent cells of the path are 8-directionally connected (i.e., they are different and they share an edge or a corner). The length of a clear path is the number of visited c

Detailed Explanation

The problem asks us to find the shortest path from the top-left corner (0, 0) to the bottom-right corner (n-1, n-1) of an n x n binary matrix. A 'clear path' consists of cells with the value 0, and adjacent cells in the path are 8-directionally connected (meaning they share an edge or a corner). The length of the path is the number of cells visited. If no such path exists, we return -1. The input is the binary matrix `grid`, and the output is the length of the shortest clear path.

Solution Approach

The solution uses Breadth-First Search (BFS) to find the shortest path. We start at the top-left cell and explore its neighbors in all 8 directions. We use a queue to store the cells to visit. Each cell in the queue is associated with its row, column, and the length of the path from the starting cell. As we visit cells, we mark them as visited by changing their value in the grid to 1. We continue until we reach the bottom-right cell or the queue is empty. If we reach the bottom-right cell, we return the path length. If the queue becomes empty and we haven't reached the destination, it means there is no clear path, so we return -1.

Step-by-Step Algorithm

  1. Step 1: Check if the starting or ending cell is blocked (value 1). If so, return -1 because no path is possible.
  2. Step 2: Initialize a queue with the starting cell (0, 0) and path length 1.
  3. Step 3: Mark the starting cell as visited by changing its value in the grid to 1.
  4. Step 4: Define an array of 8 possible directions (row and column offsets).
  5. Step 5: While the queue is not empty:
  6. Step 6: Dequeue a cell from the queue and extract its row, column, and path length.
  7. Step 7: If the dequeued cell is the destination (bottom-right corner), return the path length.
  8. Step 8: Iterate through the 8 directions:
  9. Step 9: Calculate the coordinates of the neighboring cell by adding the direction offsets to the current cell's coordinates.
  10. Step 10: Check if the neighboring cell is within the grid boundaries and is not blocked (value 0) and not visited (value 0 initially, or it's 1 if already visited during search).
  11. Step 11: If the neighboring cell is valid, mark it as visited (set its value in the grid to 1) and enqueue it with an incremented path length.
  12. Step 12: If the queue becomes empty before reaching the destination, return -1 (no path found).

Key Insights

  • Insight 1: Breadth-First Search (BFS) is the most appropriate algorithm because it guarantees finding the shortest path in an unweighted graph (where each step has the same cost).
  • Insight 2: We need to consider all 8 possible directions (up, down, left, right, and the four diagonals) when searching for adjacent cells.
  • Insight 3: Marking visited cells is crucial to avoid infinite loops and redundant calculations. We can modify the input grid itself to keep track of visited cells.

Complexity Analysis

Time Complexity: O(n^2)

Space Complexity: O(n^2)

Topics

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

Companies

Asked at: Airbnb, Intuit, Palo Alto Networks.