Reverse the flow: start from each ocean and move to higher/equal neighbors.
Run BFS/DFS twice:
height(B) >= height(A) (reverse of water flow).
Intersect the reachable sets.A cell can flow to an ocean iff the ocean can reach it by reverse edges.
Reverse the flow: start from each ocean and move to higher/equal neighbors.
Run BFS/DFS twice:
height(B) >= height(A) (reverse of water flow).
Intersect the reachable sets.A cell can flow to an ocean iff the ocean can reach it by reverse edges.