Fire in the cells.

Posted: 3 Dec, 2020
Difficulty: Hard


Try Problem

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.


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.
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.
You are not required to print the output, it has already been taken care of. Just implement the function.
1 <= 'T' <= 50
0 <= 'N' < 50
0 <= 'M' < 50
0 <= 'X' <= N
0 <= 'Y' <= M

Time Limit: 1 sec
Approach 1

With each passing second fire expands and there are multiple cells at fire initially, (at time t = 0). So we will create an auxiliary matrix ‘TIME_OF_FIRE’ of size ‘N’ * ‘M’  to store the minimum time it will take for each cell to catch fire.


Iterate through the given matrix ‘MAT’ [][] and set ‘TIME_OF_FIRE’ [][] for a cell ( ‘i’, ‘j’ ) to 0, if ‘MAT’ [‘i’][‘j’]  = 0, (source of fire) because they are on fire from the beginning only.


Fire will expand in four directions(up, down, left, right) from the fired cell and will continue to do so until all the cells are set to fire. A better choice is to use multisource BFS to store all the values of the matrix ‘TIME _OF_FIRE’ [][] for each cell in the matrix.


The steps are as follows:


  1. Initialise a ‘VISITED’ 2D array of type boolean and mark all cells to false.
  2. Those cells which are set to 0 in ‘MAT’[][], push them into a queue and mark them as visited. Also set every cell’s ‘TIME_OF_FIRE’ = ‘TIMER’.
  3. ‘TIMER’ acts as clock time.
  4. Multi-source BFS works level by level from each of its source nodes. After each second, source nodes will change. Thus when pushing any cell in the queue the minimum time for this cell to catch fire will be the minimum time taken by its parent + 1. Here the children nodes will be nothing but the adjacent cells.
  5. The next step is to find if the person can reach an escape cell and if yes the minimum time it will take for him to get out of it without being burnt.
  6. To solve this problem, initialise ‘TIMER’ = 0, and do BFS from the initial cell of the person (X, Y).
  7. How to choose, if the next cell will be possible to be visited by the person? To identify this use ‘TIMER’ + 1. If ‘TIMER’ + 1  is less than ‘TIME_OF_FIRE’ of the cell to be visited next, then that cell is possible to be visited safely, otherwise the person will get burnt. So in each iteration insert only those cells in the queue for which ‘TIMER’ + 1 is less than ‘TIME_OF_FIRE’.
  8. To identify if the person has reached escape cells via BFS, there can be these two conditions for escape cells:
    1. Either row  = 0 or row  = ‘N’ - 1:
      1. Either col >= 1 or col <= ‘M’ - 1.
    2. Either col = 0 or col = ‘M’ - 1:
      1. Either row >= 1 or row <= ‘N’ - 1.
  9. If the person escapes, then return timer value, else return -1.
Try Problem