-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNumber_Of_Enclaves.py
48 lines (42 loc) · 1.39 KB
/
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
from typing import List
class Solution:
def numberOfEnclaves(self, grid: List[List[int]]) -> int:
n=len(grid)
m=len(grid[0])
queue=[]
l=[[0 for i in range(m)] for j in range(n)]
for i in range(n):
for j in range(m):
if (i==0 or i==n-1 or j==0 or j==m-1) and grid[i][j]==1:
l[i][j]=1
queue.append([i,j])
dir1=[-1,0,1,0]
dir2=[0,1,0,-1]
while(len(queue)):
row=queue[0][0]
col=queue[0][1]
queue.pop(0)
for i in range(4):
nrow=row+dir1[i]
ncol=col+dir2[i]
if nrow>=0 and nrow<n and ncol<m and ncol>=0 and l[nrow][ncol]==0 and grid[nrow][ncol]==1:
queue.append([nrow,ncol])
l[nrow][ncol]=1
count=0
for i in range(n):
for j in range(m):
if grid[i][j]==1 and l[i][j]==0:
count+=1
return count
#{
# Driver Code Starts
#Initial Template for Python 3
if __name__ == "__main__":
for _ in range(int(input())):
n, m = map(int,input().strip().split())
grid = []
for i in range(n):
grid.append([int(i) for i in input().strip().split()])
obj=Solution()
print(obj.numberOfEnclaves(grid))
# } Driver Code Ends