-
Notifications
You must be signed in to change notification settings - Fork 0
/
Q19_pond_size.py
55 lines (45 loc) · 1.45 KB
/
Q19_pond_size.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
51
52
53
54
55
import unittest
def pond_size(matrix):
h, w = len(matrix), len(matrix[0])
visited = set()
pond_sizes = []
def dfs(start):
stack = [start]
visited.add(start)
pond_size = 0
while stack:
node = stack.pop()
pond_size += 1
neighbours = get_neighbours(node)
for (r, c) in neighbours:
if (
0 <= r < h
and 0 <= c < w
and matrix[r][c] == 0
and (r, c) not in visited
):
stack.append((r, c))
visited.add((r, c))
return pond_size
for r in range(h):
for c in range(w):
if matrix[r][c] == 0 and (r, c) not in visited:
pond_size = dfs((r, c))
pond_sizes.append(pond_size)
return pond_sizes
def get_neighbours(point):
res = []
for dr in [-1, 0, 1]:
for dc in [-1, 0, 1]:
if not (dr == 0 and dc == 0):
res.append((point[0] + dr, point[1] + dc))
return res
class Test(unittest.TestCase):
def test_get_neighbours(self):
self.assertEqual(
[(0, 0), (0, 1), (0, 2), (1, 0), (1, 2), (2, 0), (2, 1), (2, 2)],
get_neighbours((1, 1)),
)
def test_pond_size(self):
matrix = [[0, 2, 1, 0], [0, 1, 0, 1], [1, 1, 0, 1], [0, 1, 0, 1]]
self.assertEqual([2, 4, 1], pond_size(matrix))