Skip to content

Commit 4868d0f

Browse files
Chris WuChris Wu
Chris Wu
authored and
Chris Wu
committed
no message
1 parent 6ac9019 commit 4868d0f

File tree

1 file changed

+53
-33
lines changed

1 file changed

+53
-33
lines changed

problems/shortest-bridge.py

Lines changed: 53 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,70 @@
1-
import collections
1+
"""
2+
First, identify one of the island by marking its value by 2 (island2). [0]
3+
4+
Second ([1]), use BFS to go through another island (island1). When we encounter 0 we put it on `q2`, otherwise `q`.
5+
When q is finished, it means that the island1 is completely explored (all `(i, j)` is in the `visied`).
6+
Now we make put `q2` to the `q` and clear `q2`. [2]
7+
8+
When q is finished, it means that all of the one-tile outward `(i, j)` is explored (in the `visited`).
9+
Now we make put `q2` to the `q` and clear `q2`.
10+
11+
When q is finished, it means that all of the two-tile outward `(i, j)` is explored (in the `visited`).
12+
Now we make put `q2` to the `q` and clear `q2`.
13+
14+
...
15+
16+
We keep on doing this until we find the island2. [3]
17+
18+
The time complexity is `O(N)`, `N` is the number of element in `A`.
19+
The space complexity is `O(N)`, too. Since we might put most of the `(i, j)` in the `visited`.
20+
21+
Of course, we can optimize the space, by changing the value in the `A`.
22+
The code would be a little bit harder the read and understand.
23+
I challenge you to try it out!
24+
"""
225
class Solution(object):
326
def shortestBridge(self, A):
4-
def findFirst():
27+
def findFirst(target):
528
for i in xrange(M):
629
for j in xrange(N):
7-
if A[i][j]==1: return (i, j)
30+
if A[i][j]==target: return (i, j)
831

932
M, N = len(A), len(A[0])
10-
opt = float('inf')
1133

12-
q1 = collections.deque([findFirst()])
13-
while q1:
14-
i, j = q1.popleft()
34+
#[0]
35+
q = collections.deque([findFirst(1)])
36+
while q:
37+
i, j = q.popleft()
1538
if i<0 or i>=M: continue
1639
if j<0 or j>=N: continue
1740
if A[i][j]==2 or A[i][j]==0: continue
1841
if A[i][j]==1: A[i][j] = 2
19-
q1.extend([(i+1, j), (i-1, j), (i, j+1), (i, j-1)])
20-
21-
i0, j0 = findFirst()
22-
q2 = collections.deque([(i0, j0, 0)])
23-
while q2:
24-
i, j, dis = q2.popleft()
25-
if A[i][j]==3 or A[i][j]==4:
26-
continue
27-
if A[i][j]==2:
28-
opt = min(opt, dis)
29-
continue
30-
31-
if A[i][j]==1: A[i][j] = 3
32-
if A[i][j]==0: A[i][j] = 4
33-
for ni, nj in [(i+1, j), (i-1, j), (i, j+1), (i, j-1)]:
34-
if ni<0 or ni>=M: continue
35-
if nj<0 or nj>=N: continue
36-
if A[ni][nj]==1:
37-
q2.append((ni, nj, 0))
38-
elif A[ni][nj]==0:
39-
q2.append((ni, nj, dis+1))
40-
elif A[ni][nj]==2:
41-
q2.append((ni, nj, dis))
42+
q.extend([(i+1, j), (i-1, j), (i, j+1), (i, j-1)])
4243

43-
return opt
44+
#[1]
45+
q.append(findFirst(1))
46+
q2 = []
47+
tile = 0
48+
visited = set()
49+
while q or q2:
50+
if not q: #[2]
51+
q.extend(q2)
52+
q2 = []
53+
tile+=1
4454

45-
A = [[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0,0,1,1,0,1,1,0,0,0,0],[0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,0,1,1,0],[0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,0,0],[0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,0,0,0],[0,0,0,0,0,0,0,0,0,1,0,1,1,0,0,1,0,0,0,0],[0,0,0,0,0,0,0,0,0,0,0,1,0,1,1,0,1,0,0,0],[0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,1,0,0,0],[0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,1,1,1,1,0],[0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,0],[0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,0],[0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,0],[0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,0]]
46-
print Solution().shortestBridge(A)
55+
i, j = q.popleft()
56+
if (i, j) in visited: continue
57+
visited.add((i, j))
4758

59+
if A[i][j]==2: return tile #[3]
4860

61+
for ni, nj in [(i+1, j), (i-1, j), (i, j+1), (i, j-1)]:
62+
if ni<0 or ni>=M: continue
63+
if nj<0 or nj>=N: continue
64+
if A[ni][nj]==0:
65+
q2.append((ni, nj))
66+
else:
67+
q.append((ni, nj))
4968

69+
return tile #should not comes here
5070

0 commit comments

Comments
 (0)