leetcode 417 Pacific Atlantic Water Flow

Given an m x n
matrix of non-negative integers representing the height of each unit cell in a continent, the “Pacific ocean” touches the left and top edges of the matrix and the “Atlantic ocean” touches the right and bottom edges.
Water can only flow in four directions (up, down, left, or right) from a cell to another one with height equal or lower.
Find the list of grid coordinates where water can flow to both the Pacific and Atlantic ocean.
Note:
- The order of returned grid coordinates does not matter.
- Both m and n are less than 150.
Example:
1 | Given the following 5x5 matrix: |
- bfs
- 使用两个queue来分别保存已知的能够到达两个海岸坐标
- 每次从queue中去一个出来,判断他的四周四个能否同样到达他所对应的海岸
- 用两个矩阵来保存每个点能否到达对应的海岸
1 | class Solution { |