



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:
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) :
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:
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) :
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) :
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: