(row, col, matrix, visited, sizes)
| 59 | } |
| 60 | |
| 61 | function traverseNode(row, col, matrix, visited, sizes) { |
| 62 | let currentRiverSize = 0; |
| 63 | const nodesToExplore = [[row, col]]; //stack |
| 64 | |
| 65 | while (nodesToExplore.length) { |
| 66 | let currentNode = nodesToExplore.pop(); |
| 67 | row = currentNode[0]; |
| 68 | col = currentNode[1]; |
| 69 | if (visited[row][col]) continue; |
| 70 | visited[row][col] = true; |
| 71 | if (matrix[row][col] === 0) continue; |
| 72 | currentRiverSize++; |
| 73 | const unvisitedNeighbors = getUnvisitedNeighbors(row, col, matrix, visited); |
| 74 | for (let neighbor of unvisitedNeighbors) { |
| 75 | nodesToExplore.push(neighbor); |
| 76 | } |
| 77 | } |
| 78 | if (currentRiverSize > 0) sizes.push(currentRiverSize); |
| 79 | } |
| 80 | |
| 81 | function getUnvisitedNeighbors(row, col, matrix, visited) { |
| 82 | const unvisitedNeighbors = []; |
no test coverage detected