1. There is at least one cell having the integer 0 in the given matrix.
Consider the following 2*3 matrix ‘MAT’:
[0, 1, 1]
[0, 1, 1]
Here, the nearest cell having the integer 0 from the cell (0, 0) is the cell (0, 0) itself. The Manhattan distance between them is |0 - 0| + |0 - 0| = 0.
The nearest cell having the integer 0 from the cell (0, 1) is cell (0, 0). The Manhattan distance between them is |0 - 0| + |1 - 0| = 1.
The nearest cell having the integer 0 from the cell (0, 2) is cell (0, 0). The Manhattan distance between them is |0 - 0| + |2 - 0| = 2.
The nearest cell having the integer 0 from the cell (1, 0) is cell (1, 0) itself. The Manhattan distance between them is |1 - 1| + |0 - 0| = 0.
The nearest cell having the integer 0 from the cell (1, 1) is cell (1, 0). The Manhattan distance between them is |1 - 1| + |1 - 0| = 1.
The nearest cell having the integer 0 from the cell (1, 2) is cell (1, 0). The Manhattan distance between them is |1 - 1| + |2 - 0| = 2.
Thus we should return matrix:
[0, 1, 2]
[0, 1, 2]
The first line of input contains an integer ‘T’ denoting the number of test cases. then ‘T’ test cases follow.
The first line of each test case consists of two single space-separated integers ‘N’ and ‘M’ representing the number of rows and columns in matrix ‘MAT’
Then next ‘N’ lines follow in each test case. Each of these ‘N’ lines consists of ‘M’ single space-separated integers (either 0 or 1). These ‘N’ lines together represent the matrix ‘MAT’.
For each test case, print ‘N’ lines each of which consists of ‘M’ space-separated integers. The jth integer in the ith line will be the Manhattan distance of the nearest cell from the cell (i, j) in ‘MAT’ that has integer 0.
The output of each test case will be printed in a separate line.
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 50
1 <= N <= 100
1 <= M <= 100
0 <= MAT[i][j] <= 1
Where ‘T’ is the total number of test cases, ‘N’ and ‘M’ denote the number of rows and columns in the given matrix ‘MAT’, and MAT[i][j] is the element of the given matrix at cell (i, j).
Time limit: 1 sec.
This approach is straightforward, we will make a matrix ‘DIST’ of dimension N * M and initially fill it with infinity. Then we iterate over each cell of the matrix ‘MAT’ and if we find any cell in ‘MAT’ that has integer 0, then we update the value of each cell in matrix ‘DIST’ with the minimum of its current value and its Manhattan distance from the current cell of ‘MAT’.
Let consider each cell of the matrix as a graph node. The node representing cell (i, j) is connected to the nodes representing its adjacent cells, i.e cell (i - 1, j), (i, j - 1), (i, j + 1), (i + 1, j).
Now we iterate over the matrix ‘MAT’ and find all the cells that have integer 0 and then we push all these cells in the queue and run the breadth-first search algorithm in a way described below.
Let ‘DIST[i][j]’ will be the Manhattan distance of each cell from the nearest cell that have integer 0, then -:
Otherwise, ‘DIST[i][j]’ = min(‘DIST[i - 1][j]’, DIST[i][j - 1], DIST[i][j + 1], DIST[i + 1][j]) + 1.
We can compute this distance for each cell by iterating over ‘MAT’ two times, one by starting from the top left, and the other by starting from the bottom right cell as follows.
Search In A Sorted 2D Matrix
Spiral Matrix
Spiral Matrix
Spiral Matrix
Find the maximum element of each row
Find the maximum element of each row
Growing String
Growing String
Tomato Timer
Tomato Timer