Last Updated: 30 Mar, 2021

Farthest Distance From Lands

Moderate
Asked in companies
MicrosoftGoogle inc

Problem statement

You are given a binary square matrix ‘ARR’ with N rows and N columns, in which 0 represents the water and 1 represents the land.

You have to find a water cell such that its distance to the nearest land cell is maximized and print the maximum distance.

Note :

The distance between any two cells (x0, y0) and (x1, y1) is given by the Manhattan distance: |x0 - x1| + |y0 - y1|.
If no land or water exists in the grid, then return -1.
Input Format :
The first line of input contains an integer ‘T' representing the number of test cases.

The first line of each test case contains one integer ‘N’ denoting the size of the matrix.

The next ‘N’ lines contain ‘N’ integers separated by spaces describing rows of matrix ‘ARR’ (each element of ‘ARR’ is either 0 or 1).
Output Format :
For each test case, on a separate line, output one integer - the largest distance from a water cell to the nearest land cell.
Note :
You do not need to print anything. It has already been taken care of. Just implement the given function.
Constraints :
1 <= T <= 5
1 <= N <= 10^3
ARR[i][j] = 0 or 1

Time limit: 1 sec

Approaches

01 Approach

The idea here is to calculate the distance from each land cell and update every water cell to the minimum distance via any land cell.

We declare a 2d matrix minDistance[][] initially set to Infinity, and then run BFS from every land cell and calculate the shortest distance to each water cell, and update the minDistance[][] to the minimum shortest distance to a water cell. The maximum value which is not equal to infinity, of the water cell in minDistance[][] will be our ans.

The algorithm is as follows:

  • Declare an ans variable and set it to -1, representing the largest distance of a land cell from the nearest water cell.
  • Declare a dx array representing and set it to {1, 0, -1, 0}, representing the directions of all four neighboring cells.
  • Declare a dy array representing and set it to {0, 1, 0, -1}, representing the directions of all four neighboring cells.
  • Declare a 2D array minDistance[][] with N rows and N columns and set it to infinity, where minDistance[i][j] represents the minimum possible distance between a water cell from a land cell.
  • Iterate from i = 0 to N,
    • Iterate from j = 0 to N,
      • If ARR[i][j] == 1,
        • Run BFS from this land cell.
        • Compute the shortest distance from this land cell to every reachable water cell and store it to a matrix distance[][].
        • update the minDistance[][],
          • Set minDistance[i][j] = min(minDistance[i][j], distance[i][j]).
  • Iterate from i = 0 to N,
    • Iterate from j = 0 to N,
      • If ARR[i][j] == 0 and minDistance[i][j] < infinity,
        • Update ans to max(ans, minDistance[i][j]).
  • Return the ans.

02 Approach

The idea here is to think in reverse order and imagine expanding outward from each land cell i.e. using BFS from all land cells at the same time.

If we put all the land cells in the queue and then we expand in all four directions and visit the water cell one level at a time. The maximum level reached will be our ans.

 

The algorithm is as follows:

  • Declare an ans variable and set it to 0, representing the largest distance of a land cell from the nearest water cell.
  • Declare a dx array representing and set it to {1, 0, -1, 0}, representing the directions of all four neighboring cells.
  • Declare a dy array representing and set it to {0, 1, 0, -1}, representing the directions of all four neighboring cells.
  • Declare a 2D array visited with N rows and N columns, where visited[i][j] = 0 or 1, 0 representing the current cell being visited and 1 representing not visited.
  • Declare a queue of array Q, where the first element of the array is the x coordinate and the second element of the array is the y coordinate.
    • Push all the land cells to the queue.
    • Mark the land cells as visited.
  • If Q.size == 0 or Q.size == N * N,
    • Return -1.
  • While Q is not empty,
    • Declare a variable levelSize and set it to Q.size.
    • Declare a variable flag and set it to 0, representing that we can reach a new level.
    • Iterate from i = 0 to levelSize - 1,
      • Declare an array variable coordinate and set it Q.front.
      • Pop an element from the front of the queue.
      • Iterate from direction = 0 to 3,
        • Declare a variable X and set it to coordinate[0] + dx[direction].
        • Declare a variable Y and set it to coordinate[1] + dx[direction].
        • If X and Y are invalid coordinates or visited[X][Y] == 1, continue as we don’t want to visit any cell more than once.
        • Set visited[X][Y] to 1.
        • Push (X, Y) to queue Q.
        • Set flag to 1, as we reached a new level.
    • If the flag == 1,
      • Increment ans by 1.
  • Return the ans.