-
Notifications
You must be signed in to change notification settings - Fork 0
/
find_cat_11.py
52 lines (42 loc) · 1.43 KB
/
find_cat_11.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
"""
TASK: count the "cats" (the distinct shapes) on the 2d matrix. Every shape is surrounded
by the "background" zeros.
ALGORITHMS AND TECHNIQUES: deque, padding, movements' matrix, breadth-first search
"""
import sys
import numpy as np
from collections import deque
#input
cat_matrix=[]
#padding
for line in sys.stdin.readlines():
cat_matrix.append(list(map(int,line.strip('\n').split())))
cat_matrix = np.pad(cat_matrix,pad_width=1)
#create mask-matrix
dims = cat_matrix.shape
mask_matrix= np.zeros((dims),dtype=int)
## implement breadth-first algorythm ##
#function that returns the elements around coordinate (x,y)
def check_the_surroundings(x_coord,y_coord):
movements_i = [-1,1,0,0]
movements_j = [0,0,-1,1]
movements =[]
for _ in range(4):
movements.append((x_coord+movements_i[_],y_coord+movements_j[_]))
return movements
cnt=0
for i in range(len(cat_matrix)):
for j in range(len(cat_matrix[i])):
if cat_matrix[i][j]==1 and mask_matrix[i][j]==0:
cnt+=1
mask_matrix[i][j] = cnt
d = deque([(i,j)])
while d:
x_crd, y_crd = d.popleft()
for x,y in check_the_surroundings(x_crd,y_crd):
if cat_matrix[x][y]==1 and mask_matrix[x][y]==0:
d.append((x,y))
mask_matrix[x][y] = cnt
print(cnt)
for i in mask_matrix[1:-1]:
print(" ".join(list(map(str,i[1:-1]))))