-
Notifications
You must be signed in to change notification settings - Fork 1
/
1020-Number-of-Enclaves.py
53 lines (51 loc) · 1.48 KB
/
1020-Number-of-Enclaves.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
class Solution:
def numEnclaves(self, A: List[List[int]]) -> int:
h=len(A)
w=len(A[0])
direction=[(0,1),(0,-1),(1,0),(-1,0)]
def dfs(y0,x0):
A[y0][x0]=-1
for y_offset,x_offset in direction:
y1=y0+y_offset
x1=x0+x_offset
if 0<=y1<h and 0<=x1<w and A[y1][x1]==1:
dfs(y1,x1)
for y in range(h):
if A[y][0]==1:
dfs(y,0)
if A[y][w-1]==1:
dfs(y,w-1)
for x in range(w):
if A[0][x]==1:
dfs(0,x)
if A[h-1][x]==1:
dfs(h-1,x)
ret=0
for y in range(h):
for x in range(w):
if A[y][x]==1:
ret+=1
return ret
class Solution:
def numEnclaves(self, A: List[List[int]]) -> int:
h=len(A)
w=len(A[0])
direction=[(0,1),(0,-1),(1,0),(-1,0)]
def dfs(y0,x0):
A[y0][x0]=0
for y_offset,x_offset in direction:
y1=y0+y_offset
x1=x0+x_offset
if 0<=y1<h and 0<=x1<w and A[y1][x1]==1:
dfs(y1,x1)
for y in range(h):
if A[y][0]==1:
dfs(y,0)
if A[y][w-1]==1:
dfs(y,w-1)
for x in range(w):
if A[0][x]==1:
dfs(0,x)
if A[h-1][x]==1:
dfs(h-1,x)
return sum(map(sum,A))