Dsa
Graphs Bfs
Easy
Minimum Distance on Grid
You are given a 2D grid representing obstacles and free spaces. A cell with a 1 is an obstacle, and a cell with 0 is free. Your task is to find the minimum number of moves required to reach from the top-left corner to the bottom-right corner of the grid. You can move vertically or horizontally to adjacent cells. If it is impossible to reach the bottom-right corner, return -1.
Input format:
R C
0/1 ...
Where R is the number of rows (1 <= R <= 100) and C is the number of columns (1 <= C <= 100), and the next R lines contain C integers (0 or 1).
Output format:
Print the minimum number of moves or -1 if it's impossible.
Example:
Input:
3 3
0 0 0
1 0 1
0 0 0
Output:
4
Key concepts
graphsbfsgrid
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