Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding
Ninjas X Naukri.com

Last Updated: 25 Jan, 2021

Moderate

```
The first line of input contains an integer 'T' representing the number of test cases. Then the 'T' test cases are as follows.
The first line of each test case contains two single-spaced integers â€˜Nâ€™ and â€˜Mâ€™, representing the number of rows and columns of the 2-D array, respectively.
For the next 'N' lines, each line contains 'M' space-separated integers (0 or 1), where 0 denotes the water and 1 denotes the land.
```

```
For each test case, print the length of the shortest bridge which connects the two islands.
The output for each test case is 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 <= 5
1 <= N, M <= 100
0 <= ARR[i][j] <= 1
Where â€˜ARR[i][j]â€™ is the value of the elements of the 2-D array.
Time Limit: 1 sec.
```

In this solution, we will first store all coordinates of both the islands in two vectors of pairs using dfs. Then we will generate all pairs of coordinates between the two islands and find a pair of coordinates having a minimum distance between them.

The Algorithm is as follows:

- Declare a variable to store the length of the shortest bridge, say the 'ANSWER'.
- We will assign the maximum size of the bridge i.e. â€˜Nâ€™ * â€™Mâ€™ to the 'ANSWER'.
- We will maintain a 2D visited array to keep track of visited elements in â€˜DFSâ€™ function.
- Run a â€˜DFSâ€™ to store all coordinates of island1 and island2 in two vectors of pairs, say 'ISLAND1' and 'ISLAND2'.
- Run a loop and iterate over all coordinates of island1, say â€˜X1â€™ and â€˜Y1â€™.
- Run another loop and iterate over all coordinates of island2, say â€˜X2â€™ and â€˜Y2â€™.
- Distance between (â€˜X1â€™, â€˜Y1â€™) and (â€˜X2â€™, â€˜Y2â€™) is abs(â€˜X1â€™ â€“ â€˜X2â€™) + abs(â€˜Y1â€™ â€“ â€˜Y2â€™) â€“ 1. So if the distance is less than the 'ANSWER' then we will update the 'ANSWER' with distance.
- Finally, return the 'ANSWER'.

Description of DFS function to store coordinates of island1 and island2.

This function will take four arguments, â€˜Xâ€™ and â€˜Yâ€™ denoting the current coordinates, a 2-D array â€˜VISITEDâ€™ to keep track of visited coordinates/nodes and a pair of arrays/vectors to store coordinates of the current island.

DFS(X, Y, VISITED, CURISLAND) :

- If â€˜Xâ€™, â€˜Yâ€™ is out of bounds i.e. if â€˜Xâ€™ does not lie between [0, â€˜Nâ€™] or â€˜Yâ€™ does not lie between [0, â€˜Mâ€™] then return.
- If â€˜VISITED[X][Y]â€™ is true then return IT.
- If the element at â€˜Xâ€™, â€˜Yâ€™ is not equal to 1 then return.
- Add â€˜Xâ€™, â€˜Yâ€™ to curIsland.
- Add (â€˜Xâ€™, â€˜Yâ€™) to the island and mark â€˜VISITED[X][Y]â€™ as true.
- Visit all the neighbors by recursively calling in all 4 - directions of (â€˜Xâ€™, â€˜Yâ€™) i.e. (â€˜Xâ€™ â€“ 1, â€˜Yâ€™) , (â€˜Xâ€™ + 1, â€˜Yâ€™) , (â€˜Xâ€™, â€˜Yâ€™ â€“1) , (â€˜Xâ€™, â€˜Yâ€™ + 1).

The idea here is to use the flood fill algorithm. We will keep filling all 4 directional neighbours with their current value + 1 until we hit another island. We can get the answer by picking a minimum from other islands' neighbours whose values are non-zero.

See below example for better understanding.

Suppose the islands given are

Now fill all the cells of island 2 by 2. Here Island 2 is a top-right brown colored island.

Now fill all the neighbors of 2 with 3

Now fill all the neighbors of 3 with 4.

Now fill all the neighbors of 4 with 5

Here, you easily get the shortest distance of the island colored in brown by checking all its neighbors having non-zero value. Here non - zero neighbours of 1 is 5.We need to subtract 2 from 5 as we have added 2 extra in the starting of flood fill algorithm by assigning all values of the island by 2. So, in the above case, the shortest bridge size will be 3.

The Algorithm is as follows:

- Declare a variable to store the length of the shortest bridge, say the â€˜ANSWERâ€™.
- We will use a 2-D â€˜VISITEDâ€™ array to keep track of visited elements in â€˜DFSâ€™ function.
- Find a coordinate in the given array whose value is 1 and change all elements of the founded island to 2 by running a â€˜DFSâ€™.
- Call â€˜FLOODFILLâ€™ function to get the â€˜ANSWERâ€™.
- Subtract 2 from the â€˜ANSWERâ€™ as we have added 2 extra in the starting of flood fill algorithm by assigning all values of the island by 2.
- Finally, return the â€˜ANSWERâ€™.

Description of DFS function to set values of all elements of island2 to 2.

This function will take five arguments, â€˜Xâ€™ and â€˜Yâ€™ denoting the current coordinates, a 2D array â€˜VISITEDâ€™ to keep track of visited coordinates/nodes and a queue â€˜Qâ€™ to store all elements of the island

dfs(x, y, visited,q) :

- If â€˜Xâ€™, â€˜Yâ€™ is out of bounds i.e. if â€˜Xâ€™ does not lie between [0, â€˜Nâ€™] or â€˜Yâ€™ does not lie between [0, â€˜Mâ€™] then return.
- If â€˜VISITED[X][Y]â€™ is true then return.
- If the element at â€˜Xâ€™, â€˜Yâ€™ is not equal to 1 then return.
- Set the value of the element at â€˜Xâ€™, â€˜Yâ€™ to 2.
- Add (â€˜Xâ€™, â€™Yâ€™) to queue for flood Fill.
- To check all the neighbors, recursively call in all 4 - directions. of (â€˜Xâ€™, â€˜Yâ€™) i.e. (â€˜Xâ€™ â€“ 1, â€˜Yâ€™) , (â€˜Xâ€™ + 1, â€˜Yâ€™) , (â€˜Xâ€™, â€˜Yâ€™ â€“1) , (â€˜Xâ€™, â€˜Yâ€™ + 1).

Description of the Flood fill function.

This function will take four arguments, â€˜Aâ€™ - denoting the given 2-D array, â€™Nâ€™ - denoting the total number of rows, â€˜Mâ€™ - denoting the total number of columns in the array and the queue â€˜Qâ€™ which stores all elements of island 2.

floodFill(a, n, m, q) :

- Declare a direction array to visit neighbours of (â€˜Xâ€™, â€˜Yâ€™) in all four directions i.e. (â€˜Xâ€™ â€“ 1, â€˜Yâ€™) , (â€˜Xâ€™ + 1, â€˜Yâ€™) , (â€˜Xâ€™, â€˜Yâ€™ â€“ 1) , (â€˜Xâ€™, â€˜Yâ€™ + 1).
- Declare a variable â€˜VALâ€™ to maintain the current color value of neighbors.
- Run a loop until the queue is not empty.
- The size of the queue denotes the total number of nodes at the current level.
- Run a loop until you have visited all nodes of the current level.

- Pop the front item from the queue.
- Visit all 4 - directional neighbours of popped item i.e. (â€˜Xâ€™ â€“ 1, â€˜Yâ€™) , (â€˜Xâ€™ + 1, â€˜Yâ€™) , (â€˜Xâ€™, â€˜Yâ€™ â€“1) , (â€˜Xâ€™, â€˜Yâ€™ + 1).
- Say, the neighbour of the popped element is (â€˜Xâ€™, â€˜Yâ€™).
- Check If the neighbour of (â€˜Xâ€™, â€˜Yâ€™) is out of bounds i.e. if x-coordinate does not lie between [0, â€˜Nâ€™] or y-coordinate does not lie between [0, â€˜Mâ€™].
- If the value at (â€˜Xâ€™, â€˜Yâ€™) < 1 then it means we have not visited this neighbour. So visit this neighbour.
- If a neighbour of (â€˜Xâ€™, â€˜Yâ€™) equals 1, Then it means we have found the second island. Now, Return the value at (â€˜Xâ€™, â€˜Yâ€™).
- Assign a new colour to the neighbour of (â€˜Xâ€™, â€˜Yâ€™).
- Push neighbour of (â€˜Xâ€™, â€˜Yâ€™)to queue.
- Increment value of â€˜VALâ€™ by 1 for the next level.

In this solution, we will use BFS. We will use a similar idea as we used in the previous approach.

Since BFS is a level order traversal algorithm, we will first assign 2â€™s to one island using dfs then apply bfs on it. We will use a 2-D array to store the depth of nodes. We initialize the depth of all nodes to zero then we will update the depth of all unvisited neighbors by 1. If we encounter a neighbor having value 1 then we will return our answer as â€˜DEPTH[CURNODE]â€™.

The Algorithm is as follows:

- Declare two 2-D arrays, one to keep track of visited elements â€˜VISITEDâ€™ and the second one is to store the depth of the coordinates. Initialize both of them by zero.
- Declare an empty queue of pairs.
- Find a coordinate in the given array whose value is 1 and change all elements of the founded island to 2 by running a â€˜DFSâ€™ to it as done in the previous approach.
- Push the coordinates of the element found in the previous step.
- Declare a direction array to visit neighbors of (â€˜Xâ€™, â€˜Yâ€™) in all four directions i.e. (â€˜Xâ€™ â€“ 1, â€˜Yâ€™) , (â€˜Xâ€™ + 1, â€˜Yâ€™) , (â€˜Xâ€™, â€˜Yâ€™ â€“1) , (â€˜Xâ€™, â€˜Yâ€™ + 1).
- Run a loop until the queue is not empty.
- Pop the front item from the queue and mark it as visited true.
- Visit all 4 - directional neighbors of the popped item i.e. (â€˜Xâ€™ â€“ 1, â€˜Yâ€™) , (â€˜Xâ€™ + 1, â€˜Yâ€™) , (â€˜Xâ€™, â€˜Yâ€™ â€“1) , (â€˜Xâ€™, â€˜Yâ€™ + 1).
- Say, the neighbor of the popped element is (â€˜Xâ€™, â€˜Yâ€™).
- If â€˜Xâ€™, â€˜Yâ€™ is out of bounds i.e. if â€˜Xâ€™ does not lie between [0, â€˜Nâ€™] or â€˜Yâ€™ does not lie between [0, â€˜Mâ€™] then continue.
- If (â€˜Xâ€™, â€˜Yâ€™) is visited then continue.
- If (â€˜Xâ€™, â€˜Yâ€™) is equal to 1, then return â€˜DEPTHâ€™ of the popped item.
- Else assign â€˜DEPTH[X][Y]â€™ as the depth of popped item + 1 and push (â€˜Xâ€™, â€˜Yâ€™) to queue.