MCPcopy
hub / github.com/austingebauer/go-leetcode / canReachAtlantic

Function canReachAtlantic

pacific_atlantic_water_flow_417/solution.go:120–154  ·  view source on GitHub ↗
(matrix [][]int, r, c, pr, pc int)

Source from the content-addressed store, hash-verified

118}
119
120func canReachAtlantic(matrix [][]int, r, c, pr, pc int) bool {
121 if reachedPacific(r, c) {
122 return false
123 }
124
125 if reachedAtlantic(matrix, r, c) {
126 return true
127 }
128
129 // go north
130 if waterCanFlow(matrix, pr, pc, r, c, r-1, c) &&
131 canReachAtlantic(matrix, r-1, c, r, c) {
132 return true
133 }
134
135 // go south
136 if waterCanFlow(matrix, pr, pc, r, c, r+1, c) &&
137 canReachAtlantic(matrix, r+1, c, r, c) {
138 return true
139 }
140
141 // go east
142 if waterCanFlow(matrix, pr, pc, r, c, r, c+1) &&
143 canReachAtlantic(matrix, r, c+1, r, c) {
144 return true
145 }
146
147 // go west
148 if waterCanFlow(matrix, pr, pc, r, c, r, c-1) &&
149 canReachAtlantic(matrix, r, c-1, r, c) {
150 return true
151 }
152
153 return false
154}
155
156func reachedAtlantic(matrix [][]int, r, c int) bool {
157 return r >= len(matrix) || (r > -1 && c >= len(matrix[r]))

Callers 1

pacificAtlantic0Function · 0.85

Calls 3

reachedPacificFunction · 0.85
reachedAtlanticFunction · 0.85
waterCanFlowFunction · 0.85

Tested by

no test coverage detected