-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsolution.py
37 lines (34 loc) · 1.19 KB
/
solution.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
"""
69 / 69 test cases passed.
Runtime: 124 ms
Memory Usage: 18 MB
"""
class Solution:
def shortestBridge(self, grid: List[List[int]]) -> int:
m, n = len(grid), len(grid[0])
que = collections.deque()
def dfs(x, y):
grid[x][y] = 2
que.append((x, y))
for xx, yy in [(x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1)]:
if 0 <= xx < m and 0 <= yy < n and grid[xx][yy] == 1:
dfs(xx, yy)
def bfs():
distance = -1
while que:
distance += 1
for _ in range(len(que)):
x, y = que.popleft()
for xx, yy in [(x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1)]:
if 0 <= xx < m and 0 <= yy < n and grid[xx][yy] != 2:
if grid[xx][yy] == 1:
return distance
grid[xx][yy] = 2
que.append((xx, yy))
return -1
for i in range(m):
for j in range(n):
if grid[i][j]:
dfs(i, j)
return bfs()
return -1