
If the given grid is this:
[0, 0, 0]
[0, 0, 1]
Then the modified grid will be
[0, 0, 1],
[1, 1, 1]
The first line contains an integer ‘T’ denoting the number of test cases. Then each test case follows.
The first input line of each test case contains two space integers‘ N’ and ‘M’ representing the number of rows and columns of the grid, respectively.
The next 'N' lines represent the rows of the grid. Every row contains M single space-separated integers.
For each test case, print the modified grid.
Print the output of each test case in a separate line.
You are not required to print the expected output; it has already been taken care of. Just implement the function.
Also, update the given matrix in place.
1 <= T <= 100
1 <= N <= 50
1 <= M <= 50
0 <= MAT[i][j] <= 1
Time limit: 1 sec
We will traverse the whole grid and whenever MAT[i][j] == 1, we will mark all 0s in the i-th row and j-th column as -1. This will help us in differentiating between the 1s that are initially there in the matrix and the 1s which we will set now.
Now, traverse the grid again and mark the cells with value -1 as 1.
We will store the row number and column number of cells having value 1 and then mark all the cells of recorded row and columns as 1 in the next iteration.
Algorithm
The main idea is that we will use the first cell of every row and column as an indicator that the row or column has any cell with value 1 or not.
We will use two additional variables ‘firstColumn’ to tell us if the first column contains a cell with value 1 or not and ‘firstRow’ would be used to tell the same for the first row.
Algorithm