Dsa
Graphs Bfs
Hard
Minimum Steps to Escape
You are trapped in a maze represented as a 2D grid filled with 0s and 1s. The 0s represent open paths and the 1s represent walls. Your goal is to find the minimum number of steps required to escape from the top-left corner (0, 0) to the bottom-right corner (R-1, C-1). If it is impossible to escape, return -1.
Input Format:
- The first line contains two integers R (rows) and C (columns).
- The next R lines contain C integers each, where each integer is either 0 (open path) or 1 (wall).
Output Format:
- Print the minimum steps to reach the exit or -1 if it's impossible.
Example:
Input:
3 3
0 1 0
0 0 0
1 0 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