-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0507.py
More file actions
34 lines (32 loc) · 929 Bytes
/
0507.py
File metadata and controls
34 lines (32 loc) · 929 Bytes
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
from collections import deque
def solution(arr, N):
que = deque()
que.append((0,0))
visit = [[False] * N for _ in range(N)]
cond = False
count = []
dx = [-1,+1,0,0]
dy = [0,0,-1,+1]
result = 0
while que:
x,y = que
visit[x][y] = True
for j in range(4):
x = dx[j] + x
y = dy[j] + y
visited = visit[x][y]
if x > N or x < 0 or y > N or y < 0:
continue
else:
if arr[x][y] != 0 and visited != True:
result += 1
que.append((x,y))
elif arr[x][y] == 0 and visited != True:
if result == 0:
result = 0
else:
count.append(result)
continue
else:
break
return count.sort()