Fire in the Cells Problem Statement

Given a matrix MAT of size N * M, where each cell is either on fire or safe, determine if a person can reach an escape cell without being burnt. The person starts from a given position and can move to adjacent cells every second. Fire spreads to adjacent cells each second too.

Input:

An integer 'T' representing the number of test cases.
For each test case, the first line contains two integers, 'N' and 'M', representing the dimensions of the matrix.
The next 'N' lines each contain 'M' integers (0 or 1), representing the matrix's initial state.
The last line contains two integers 'X' and 'Y', the starting position of the person.

Output:

An integer representing the time taken to reach an escape cell if possible, otherwise return -1. Print a separate result for each test case.

Example:

Input:
1
3 3
1 0 1
1 1 1
1 1 1
1 1
Output:
-1
Explanation:

The person starts from cell (1, 1) but cannot reach an escape cell without encountering fire.

Constraints:

  • 1 <= 'T' <= 50
  • 0 <= 'N' < 50
  • 0 <= 'M' < 50
  • 0 <= 'X' < N
  • 0 <= 'Y' < M

Note:

Escape cells are cells at the edge of the matrix, but not at the corners. Fire in a cell is permanent and spreads to adjacent non-fire cells.

Be the first one to answer
Add answer anonymously...
Amazon Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

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

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter