## Saturday, November 12, 2016

### Nearest 1 in a binary matrix - GeeksforGeeks

Nearest 1 in a binary matrix - GeeksforGeeks
Given a binary matrix of order m*n, the task is to find the distance of nearest 1 for each 0 in the matrix and print final distance matrix. From any cell (i,j), we can move only in four directions up, down, left and right.
Note : Distance from one cell to immediate another cell is always incremented by 1.

simple solution for this problem is to for each 0 in the matrix recursively check the nearest 1 in the matrix.
An efficient solution solution for this problem is to use BFS. Here is the algorithm to solve this problem :

• Take distance matrix dist[m][n] and initialize it with INT_MAX.
• Now traverse the matrix and make_pair(i,j) of inidices of cell (i, j) having value ‘1’ and push this pair into queue and update dist[i][j] = 0 because distance of ‘1’ from itself will be always 0.
• Now pop elements from queue one by one until it gets empty and call BFS on it.
• Here we need to find the distance of nearest one and we are calling BFS for the cells having ‘1’, so whenever we take adjacent of popped element from queue, we try to minimize the distance by putting condition if (dist[i][j]+1) < dist[ADCi][ADCj]. Then we update the distance of adjacent element in the distance matrix and push this adjacent in the queue to complete the BFS traversal and filling the complete distance matrix.
• After completing the BFS traversal each cell of distance matrix will contain the distance of nearest ‘1’.
`// distance matrix which stores the distnace of`
`// nearest '1'`
`int` `dist[MAX][MAX];`

`// Function to find the nearest '1'`
`void` `nearestOne(``int` `mat[][MAX], ``int` `m, ``int` `n)`
`{`
`    ``// two array when respective values of newx and`
`    ``// newy are added to (i,j) it gives up, down,`
`    ``// left or right adjacent of (i,j) cell`
`    ``int` `newx[] = {-1, 0, 1, 0};`
`    ``int` `newy[] = {0, -1, 0, 1};`

`    ``// queue of pairs to store nodes for bfs`
`    ``queue< pair<``int``,``int``> > q;`

`    ``// traverse matrix and make pair of indices of`
`    ``// cell (i,j) having value '1' and push them`
`    ``// in queue`
`    ``for` `(``int` `i=0; i<m; i++)`
`    ``{`
`        ``for` `(``int` `j=0; j<n; j++)`
`        ``{`
`            ``dist[i][j] = INT_MAX;`

`            ``if` `(mat[i][j] == 1)`
`            ``{`
`                ``// distace of '1' from itself is always 0`
`                ``dist[i][j] = 0;`

`                ``// make pair and push it in queue`
`                ``q.push(make_pair(i, j));`
`            ``}`
`        ``}`
`    ``}`

`    ``// now do bfs traversal`
`    ``// pop element from queue one by one untill it gets empty`
`    ``// pair element to hold the currently poped element`
`    ``pair<``int` `,``int``> poped;`
`    ``while` `(!q.empty())`
`    ``{`
`        ``poped = q.front();`
`        ``q.pop();`

`        ``// coordinate of currently poped node`
`        ``int` `x = poped.first;`
`        ``int` `y = poped.second;`

`        ``// now check for all adjancent of poped element`
`        ``for` `(``int` `i=0; i<4; i++)`
`        ``{`
`            ``int` `adjx = x + newx[i];`
`            ``int` `adjy = y + newy[i];`

`            ``// if new coordinates are within boundry and`
`            ``// we can minimize the distance of adjancent`
`            ``// then update the distnace of adjacent in`
`            ``// distance matrix and push this adjacent`
`            ``// element in queue for further bfs`
`            ``if` `(adjx>=0 && adjx<m && adjy>=0 && adjy<n &&`
`                    ``dist[adjx][adjy] > dist[x][y] + 1)`
`            ``{`
`                ``// update distance`
`                ``dist[adjx][adjy] = dist[x][y] + 1;`
`                ``q.push(make_pair(adjx,adjy));`
`            ``}`
`        ``}`
`    ``}`
`}`