-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack_count.py
More file actions
21 lines (21 loc) · 758 Bytes
/
stack_count.py
File metadata and controls
21 lines (21 loc) · 758 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
def stack_count(arr):
position_array=[]
for i in range(len(arr)):
for j in range(len(arr[0])):
if arr[i][j]!=0:
position_array.append((i,j))
all_matrix=[[position_array.pop(0)]]
remove_list=[]
while position_array:
for i in position_array:
for j in all_matrix:
if (i[0]-1,i[1]) in j or (i[0],i[1]-1) in j or (i[0]+1,i[1]) in j or (i[0],i[1]+1) in j:
j.append(i)
remove_list.append(i)
break
if remove_list:
position_array=list(set(position_array)-set(remove_list))
remove_list=[]
else:
all_matrix.append([position_array.pop(0)])
return len(all_matrix)