Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com

Fire in the cells.

Hard
0/120
Average time to solve is 15m
profile
Contributed by
52 upvotes
Asked in companies
AmazonPaytm (One97 Communications Limited)VMware Inc

Problem statement

You are given a matrix 'MAT' of size ‘N’ * ‘M’, where ‘N’ is the number of rows and ‘M’ is the number of columns. Value ‘0’ in a cell represents that the cell is set on fire initially, (at time t = ‘0’), and the cells which don’t have fire initially have value = ‘1’, and are called safe cells.

If a cell is on fire, then in one second the fire will expand to its adjacent cells (left, right, top, bottom) if they are not already on fire.

You are given the position of a person as integers ‘X’ and ‘Y’ denoting the cell (‘X’, ‘Y’). In one second the person can move from its current cell to any one of its adjacent cells, provided they are not on fire.

You have to determine if the person can move through the matrix to one of the escape cells without burning himself i.e. without going through the fire cells. If it’s possible, return time taken, else return -1.

Note:

1. Escape cells in this problem are all such cells that lie on the edge of the matrix but not on the corner. i.e all such cells which are in the first row, first column, last row, and last column excluding the four corner cells are considered as valid escape cells.

2. A cell once set on fire continues to remain on fire till the end.

3. Note that rows are indexed from 0 to ‘N’ - 1 and columns are indexed from 0 to ‘M’ - 1.

4. The escape cells may also be initially on fire or can catch fire from some neighboring cell.
Detailed explanation ( Input/output format, Notes, Images )
Input Format:
The first line of the input contains an integer ‘T’ denoting the number of test cases.

The first line of each test case contains two space-separated integers ‘N’ and ‘M’, where ‘N’ denotes the number of rows in the given matrix and ‘M’ denotes the number of columns in the given matrix.

Each of the next ‘N’ lines contains ‘M’ space-separated integers (‘0’ or ‘1’) denoting the initial condition of cells.

The last line of each test case contains two space-separated integers ‘X’ and ‘Y’, where ‘X’ denotes the initial row of the person at time t = 0 and ‘Y’ denotes the initial column of the person at time t = 0.
Output Format:
For each test case, print an integer ‘K’ representing the time taken by the person to reach the escape cell. If the person cannot reach the escape cell then print -1.

Output for each test case will be printed in a separate line.
Note:
You are not required to print the output, it has already been taken care of. Just implement the function.
Constraints:
1 <= 'T' <= 50
0 <= 'N' < 50
0 <= 'M' < 50
0 <= 'X' <= N
0 <= 'Y' <= M

Time Limit: 1 sec
Sample Input 1:
1
5 4
0 1 1 1
1 0 1 1
1 1 1 1
0 1 1 1
0 0 0 0
2 1
Sample Output 1:
-1
Explanation For Sample Output 1:
The time for the fire to reach the individual cells of the given matrix is as follows:

The person’s initial cell is (2, 1).
At time t = 1, the person can only go to cell (2, 2) as the fire won’t have reached the cell yet.
At time t = 2, the person can not go anywhere as all the adjacent cells are at the fire, and moving causes him to get burnt.
So the answer is -1.
Sample Input 2:
1
4 4
0 1 1 1
1 0 1 1
1 1 1 1
0 1 1 0
1 2
Sample Output 2:
1   
Full screen
Console