Shortest Bridge
In a given 2D binary arrayA
, there are two islands. (An island is a 4-directionally connected group of 1
s not connected to any other 1s.)
Now, we may change0
s to1
s so as to connect the two islands together to form 1 island.
Return the smallest number of0
s that must be flipped. (It is guaranteed that the answer is at least 1.)
Example 1:
Example 2:
Example 3:
Note:
分析
dfs+bfs
先dfs把第一个岛都标记visited并且塞进q
再bfs一层层扩展,记得遇到1直接返回。还有入栈时候标记visited
Last updated