417. Pacific Atlantic Water Flow
There is an m x n rectangular island that borders both the Pacific Ocean and Atlantic Ocean. The Pacific Ocean touches the island's left and top edges, and the Atlantic Ocean touches the island's right and bottom edges.
The island is partitioned into a grid of square cells. You are given an m x n integer matrix heights where heights[r][c] represents the height above sea level of the cell at coordinate (r, c).
The island receives a lot of rain, and the rain water can flow to neighboring cells directly north, south, east, and west if the neighboring cell's height is less than or equal to the current cell's height. Water can flow from any cell adjacent to an ocean into the ocean.
Return a 2D list of grid coordinates result where result[i] = [ri, ci] denotes that rain water can flow from cell (ri, ci) to both the Pacific and Atlantic oceans.
Constraints:
- m == heights.length
- n == heights[r].length
- 1 <= m, n <= 200
- 0 <= heights[r][c] <= 10^5
Example
Input: heights = [[1,2,2,3,5],[3,2,3,4,4],[2,4,5,3,1],[6,7,1,4,5],[5,1,1,2,4]]
Output: [[0,4],[1,3],[1,4],[2,2],[3,0],[3,1],[4,0]]
Explanation: The following cells can flow to the Pacific and Atlantic oceans, as shown below:
[0,4]: [0,4] -> Pacific Ocean
[0,4] -> Atlantic Ocean
[1,3]: [1,3] -> [0,3] -> Pacific Ocean
[1,3] -> [1,4] -> Atlantic Ocean
[1,4]: [1,4] -> [1,3] -> [0,3] -> Pacific Ocean
[1,4] -> Atlantic Ocean
[2,2]: [2,2] -> [1,2] -> [0,2] -> Pacific Ocean
[2,2] -> [2,3] -> [2,4] -> Atlantic Ocean
[3,0]: [3,0] -> Pacific Ocean
[3,0] -> [4,0] -> Atlantic Ocean
[3,1]: [3,1] -> [3,0] -> Pacific Ocean
[3,1] -> [4,1] -> Atlantic Ocean
[4,0]: [4,0] -> Pacific Ocean
[4,0] -> Atlantic Ocean
Note that there are other possible paths for these cells to flow to the Pacific and Atlantic oceans.
思路
Pacific 是左面和上面,Atlantic 是右面和下面,利用深度遍历分别找出两种各自能流到的所有格子,最后的结果就是他们的交集
题解
public List<List<Integer>> pacificAtlantic(int[][] heights) {
int row, col;
HashSet<List<Integer>> pacificVisit = new HashSet<>();
HashSet<List<Integer>> atlanticVisit = new HashSet<>();
row = heights.length;
col = heights[0].length;
for (int i = 0; i < row; i++) {
// 左面
dfsAtlanticPacific(i, 0, pacificVisit, heights[i][0], heights);
// 右面
dfsAtlanticPacific(i, col - 1, atlanticVisit, heights[i][col - 1], heights);
}
for (int i = 0; i < col; i++) {
// 上面
dfsAtlanticPacific(0, i, pacificVisit, heights[0][i], heights);
// 下面
dfsAtlanticPacific(row - 1, i, atlanticVisit, heights[row - 1][i], heights);
}
// 找出交集
return pacificVisit.stream().filter(atlanticVisit::contains).collect(Collectors.toList());
}
private void dfsAtlanticPacific(int row, int col, HashSet<List<Integer>> visit, int pre, int[][] heights) {
if (row < 0 || row >= heights.length || col < 0 || col >= heights[0].length ||
// 当前路径遍历过直接跳
visit.contains(Arrays.asList(row, col)) ||
// 这个条件很关键,如果上一个节点大于当前节点,因为整体遍历的趋势是从外往内的,里面的比外面小,就代表水无法往外流,那就跳过
heights[row][col] < pre)
return;
visit.add(Arrays.asList(row, col));
dfsAtlanticPacific(row + 1, col, visit, heights[row][col], heights);
dfsAtlanticPacific(row - 1, col, visit, heights[row][col], heights);
dfsAtlanticPacific(row, col + 1, visit, heights[row][col], heights);
dfsAtlanticPacific(row, col - 1, visit, heights[row][col], heights);
}
标签:Medium,Flow,Pacific,heights,Atlantic,Ocean,col,row
From: https://www.cnblogs.com/tanhaoo/p/17152787.html