-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathq16.19.py
40 lines (29 loc) · 1.26 KB
/
q16.19.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
def get_pond_size(matrix, row_index, col_index):
# print("checking:", row_index, col_index, matrix[row_index][col_index] )
if row_index >= len(matrix) or row_index < 0 or col_index >= len(matrix[0]) or col_index < 0 or matrix[row_index][col_index] != 0:
return 0
else:
matrix[row_index][col_index] = -1
return get_pond_size(matrix, row_index, col_index+1) + \
get_pond_size(matrix, row_index, col_index-1) + \
get_pond_size(matrix, row_index+1, col_index) + \
get_pond_size(matrix, row_index-1, col_index) + \
get_pond_size(matrix, row_index-1, col_index-1) + \
get_pond_size(matrix, row_index+1, col_index-1) + \
get_pond_size(matrix, row_index-1, col_index+1) + \
get_pond_size(matrix, row_index+1, col_index+1) + 1
def get_connected_pond_sizes(matrix):
if len(matrix) == 0 or len(matrix[0]) == 0:
return None
ret_val = []
for row_index in range(len(matrix)):
for col_index in range(len(matrix[0])):
if matrix[row_index][col_index] == 0:
ret_val.append(get_pond_size(matrix, row_index, col_index))
return ret_val
if __name__ == '__main__':
matrix = [[0,2,1,0], [0,1,0,1], [1,1,0,1], [0,1,0,1]]
for it in matrix:
print(it)
ret_val = get_connected_pond_sizes(matrix)
print(ret_val)