Dsa
Graphs Bfs
Medium
Shortest Path in Maze
You are given a 2D grid representing a maze where '0' is a free cell and '1' is a wall. Your task is to find the length of the shortest path from the top-left corner (0, 0) to the bottom-right corner (m-1, n-1). You can move up, down, left, or right. If there is no valid path, return -1.
Input format: The first line contains two integers m and n (1 <= m, n <= 100). The next m lines each contain n integers (0 or 1).
Output format: An integer representing the length of the shortest path or -1 if no path exists.
Example:
Input:
3 3
0 1 0
0 0 0
1 1 0
Output:
4
Key concepts
graphsbfsshortest path
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now