Given a binary grid containing exactly two islands, return the minimum number of 0s that must be flipped to connect the two islands. Pattern focus: Multi-Source BFS. Mark one island first, then expand from its boundary cells until the second island is reached.
grid = binary matrix with exactly two islands
minimum number of flips needed to connect the islands
Example 1:
Input:
grid = [[0,1],[1,0]]
Output:
1
Explanation:
Flipping one water cell connects the islands.
Example 2:
Input:
grid = [[0,1,0],[0,0,0],[0,0,1]]
Output:
2
Explanation:
Two flips connect the islands by the shortest route.