Shortest Path in a Binary Maze

Find the length of the shortest path in a binary maze given in the form of a rectangular matrix of size M*N, where each element is 0 or 1. Calculate the shortest path from a designated source cell to a destination cell if one exists.

Explanation:

The path can only be made if the cell value is 1, and movements can occur in the following four directions:

Up: (x, y) -> (x - 1, y)
Left: (x, y) -> (x, y - 1)
Down: (x, y) -> (x + 1, y)
Right: (x, y) -> (x, y + 1)

If no path exists from the source to the destination, return -1.

Input:

Integer T, the number of test cases.
For each test case:
- Two integers M and N, representing rows and columns.
- M lines with N space-separated integers, representing the matrix.
- One line with two integers, the source cell coordinates.
- One line with two integers, the destination cell coordinates.

Output:

For each test case, a single integer representing the shortest path's length, or -1 if no path exists.

Example:

Input:
1
3 4
1 1 1 1
0 1 1 0
0 0 1 1
0 0
2 3

Output:
5

Constraints:

  • 1 <= T <= 10
  • 1 <= M, N <= 100

Time Limit: 1 sec

AnswerBot
4mo

Find the length of the shortest path in a binary maze from source to destination.

  • Use BFS algorithm to find the shortest path in the binary maze.

  • Create a visited matrix to keep track of visited cells.

  • C...read more

Help your peers!
Select
Add answer anonymously...

Amazon Software Developer interview questions & answers

A Software Developer was asked 1mo agoQ. What is HTML?
A Software Developer was asked 1mo agoQ. What is MySQL?
A Software Developer was asked 2mo agoQ. Given two strings s and t, return true if they are equal when both are typed int...read more

Popular interview questions of Software Developer

A Software Developer was asked 1mo agoQ1. What is HTML?
A Software Developer was asked 1mo agoQ2. What is MySQL?
A Software Developer was asked 2mo agoQ3. What is the system design for the cart feature in an e-commerce website?

Top HR questions asked in Amazon Software Developer

A Software Developer was asked 1mo agoQ1. Tell me about a time you had to get to the root cause of a problem
A Software Developer was asked 5mo agoQ2. What are the short-term and long-term goals for the team or organization?
A Software Developer was asked 5mo agoQ3. Why do you want to work at Amazon?
Amazon Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits