Dsa
Graphs Bfs
Easy
Minimum Steps to Destination
You are given a grid of size n x n (1-indexed) where 0 represents an empty cell and 1 represents an obstacle. Write a program to find the minimum number of steps required to move from the top-left corner (1, 1) to the bottom-right corner (n, n). You can only move right, down, or diagonally to the bottom right. If it's not possible to reach the destination, return -1.
Input format:
The first line contains an integer n (the size of the grid).
The next n lines contain n integers (0 or 1) representing the grid.
Output format:
Print a single integer representing the minimum number of steps or -1 if unreachable.
Example:
Input:
3
0 0 0
0 1 0
0 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