-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshortestBridge.py
More file actions
44 lines (38 loc) · 1.3 KB
/
shortestBridge.py
File metadata and controls
44 lines (38 loc) · 1.3 KB
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
38
39
40
41
42
43
44
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# Source: https://leetcode.com/problems/shortest-bridge/
# Author: Miao Zhang
# Date: 2021-03-26
class Solution:
def shortestBridge(self, A: List[List[int]]) -> int:
n = len(A)
q = collections.deque()
def dfs(i, j):
A[i][j] = -1
q.append((i, j))
for x, y in [(i + 1, j), (i - 1, j), (i, j - 1), (i, j + 1)]:
if 0 <= x < n and 0 <= y < n and A[x][y] == 1:
dfs(x, y)
def find_first_land():
for i in range(n):
for j in range(n):
if A[i][j]:
dfs(i, j)
return
dirs = [(1, 0), (-1, 0), (0, 1), (0, -1)]
find_first_land()
step = 0
while q:
lenq = len(q)
for _ in range(lenq):
i, j = q.popleft()
for d in dirs:
x = i + d[0]
y = j + d[1]
if 0 <= x < n and 0 <= y < n:
if A[x][y] == 1: return step
if A[x][y] == 0:
A[x][y] = -1
q.append((x, y))
step += 1
return step