-
Notifications
You must be signed in to change notification settings - Fork 0
/
934.py
58 lines (50 loc) · 1.85 KB
/
934.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
class Solution(object):
def shortestBridge(self, A):
"""
:type A: List[List[int]]
:rtype: int
"""
group1 = []
select = [[0 for i in A[0]] for j in A]
found = False
for i in range(len(A)):
for j in range(len(A[i])):
if A[i][j] == 1:
if len(group1) == 0:
self.findgroup(group1, i, j, A, select)
found = True
break
if found:
break
pick = group1
count = 0
while pick:
new_pick = set()
directions = [[-1, 0], [1, 0], [0, -1], [0, 1]]
print pick,count
for [i, j] in pick:
for [m, n] in directions:
if len(A) > i + m >= 0 and len(A) > j + n >= 0:
if A[i + m][j + n] == 1:
return count
elif A[i + m][j + n] == 0:
A[i + m][j + n] = 2
new_pick.add((i + m, j + n))
pick = new_pick
count += 1
def findgroup(self, group, i, j, A, select):
directions = [[-1, 0], [1, 0], [0, -1], [0, 1]]
if select[i][j] == 0:
select[i][j] = 1
inner = True
for [m, n] in directions:
if len(A) > i + m >= 0 and len(A) > j + n >= 0 and A[i + m][j + n] == 0:
inner = False
A[i][j] = 2 if not inner else 3
if not inner:
group.append([i, j])
for [m, n] in directions:
if len(A) > i + m >= 0 and len(A) > j + n >= 0 and A[i + m][j + n] == 1:
self.findgroup(group, i + m, j + n, A, select)
solu = Solution()
print solu.shortestBridge([[0, 1, 0], [0, 0, 0], [0, 0, 1]])