Shortest Path in Binary Matrix Problem

Shortest Path in Binary Matrix Problem — ExecCode Medium DSA Practice

Solve the Shortest Path in Binary Matrix problem on ExecCode. Free online medium DSA practice in Arrays - Basics. Write and run code in Java, C++, Python — no signup required to run.

Problem description

Given an n×n binary matrix grid, return the length of the shortest clear path from top-left to bottom-right. A clear path visits only 0 cells and may move 8-directionally. Return -1 if no path exists.

Examples

Input grid = [[0, 0, 0], [1, 1, 0], [1, 1, 0]]; Output 4. Input grid = [[0, 1], [1, 0]]; Output 2. Input grid = [[1, 0], [0, 0]]; Output -1

Constraints

n == grid.length == grid[i].length 1 ≤ n ≤ 100 grid[i][j] is 0 or 1

Practice Shortest Path in Binary Matrix free on ExecCode. Browse DSA problems, topic map, and placement guides.