-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathriverSize.py
43 lines (39 loc) · 1.36 KB
/
riverSize.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
def riverSizes(matrix):
sizes = []
visited = [[False for value in row] for row in matrix]
for i in range(len(matrix)):
for j in range(len(matrix[i])):
if visited[i][j]:
continue
traverseNode(i, j, matrix, visited, sizes)
return sizes
def traverseNode(i, j, matrix,visited, sizes):
currentRiverSize = 0
nodesToExplore = [[i, j]]
while len(nodesToExplore):
currentNode = nodesToExplore.pop()
i = currentNode[0]
j = currentNode[1]
if visited[i][j]:
continue
visited[i][j] = True
if matrix[i][j] == 0:
continue
currentRiverSize += 1
unVisitedNeighbors = getUnvisitedNeighbours(
i, j, matrix, visited)
for neighbor in unVisitedNeighbors:
nodesToExplore.append(neighbor)
if currentRiverSize > 0:
sizes.append(currentRiverSize)
def getUnvisitedNeighbours(i, j, matrix, visited):
unvisitedNeighbors = []
if i > 0 and not visited[i-1][j]:
unvisitedNeighbors.append([i-1, j])
if i < len(matrix)-1 and not visited[i+1][j]:
unvisitedNeighbors.append([i+1, j])
if j > 0 and not visited[i][j-1]:
unvisitedNeighbors.append([i, j-1])
if j < len(matrix[0])-1 and not visited[i][j+1]:
unvisitedNeighbors.append([i, j+1])
return unvisitedNeighbors