> For the complete documentation index, see [llms.txt](https://nataliekung.gitbook.io/ladder_code/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://nataliekung.gitbook.io/ladder_code/dfs/shortest-bridge.md).

# Shortest Bridge

In a given 2D binary array`A`, there are two islands. (An island is a 4-directionally connected group of `1`s not connected to any other 1s.)

Now, we may change`0`s to`1`s so as to connect the two islands together to form 1 island.

Return the smallest number of`0`s that must be flipped. (It is guaranteed that the answer is at least 1.)

**Example 1:**

```
Input: 
[[0,1],[1,0]]
Output: 
1
```

**Example 2:**

```
Input: 
[[0,1,0],[0,0,0],[0,0,1]]
Output: 
2
```

**Example 3:**

```
Input: 
[[1,1,1,1,1],[1,0,0,0,1],[1,0,1,0,1],[1,0,0,0,1],[1,1,1,1,1]]
Output: 
1
```

**Note:**

```
1 <= A.length = A[0].length <= 100
A[i][j] == 0 or A[i][j] == 1
```

分析

dfs+bfs

先dfs把第一个岛都标记visited并且塞进q

再bfs一层层扩展，记得遇到1直接返回。还有入栈时候标记visited

```
class Solution:
    def shortestBridge(self, A: List[List[int]]) -> int:

        res = float('inf')
        d = [-1,0,1,0,-1]
        n,m = len(A),len(A[0])

        def dfs(x,y):
            if A[x][y] != 1:
                return
            A[x][y] = -1
            q.append((x,y))
            for nx,ny in [(x+d[k],y+d[k+1]) for k in range(4)]:
                if 0<=nx<n and 0<=ny<m and A[nx][ny] == 1:
                    dfs(nx,ny)
        q = []           
        found = False          
        for u in range(n): 
            if found:
                break
            for v in range(m):
                if A[u][v] == 1:
                    dfs(u,v)
                    found = True
                    break

        step = 0
        while q:            
            size = len(q)
            for k in range(size):
                i,j = q.pop(0)  
                for nx,ny in [(i+d[k],j+d[k+1]) for k in range(4)]:
                    if 0<=nx<n and 0<=ny<m and A[nx][ny] != -1:
                        if A[nx][ny] == 1:
                            return step
                        q.append((nx,ny))
                        A[nx][ny] = -1 #入栈时标记visited
            step += 1
        return step
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://nataliekung.gitbook.io/ladder_code/dfs/shortest-bridge.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
