| 50 | } |
| 51 | |
| 52 | func explore(matrix [][]int, visited *[][]bool, prev, r, c int) { |
| 53 | v := *visited |
| 54 | |
| 55 | // if we can't travel further, then return |
| 56 | if r < 0 || c < 0 || |
| 57 | r >= len(matrix) || c >= len(matrix[0]) || |
| 58 | v[r][c] || matrix[r][c] < prev { |
| 59 | return |
| 60 | } |
| 61 | |
| 62 | v[r][c] = true |
| 63 | explore(matrix, visited, matrix[r][c], r-1, c) // north |
| 64 | explore(matrix, visited, matrix[r][c], r+1, c) // south |
| 65 | explore(matrix, visited, matrix[r][c], r, c+1) // east |
| 66 | explore(matrix, visited, matrix[r][c], r, c-1) // west |
| 67 | } |
| 68 | |
| 69 | // Initial solution: for each coordinate, see if you can reach |
| 70 | // both the pacific and the atlantic. Correct solution, but is slow. |