题目:
题解:
class Solution {
static int[][] dirs = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
int[][] heights;
int m, n;
public List<List<Integer>> pacificAtlantic(int[][] heights) {
this.heights = heights;
this.m = heights.length;
this.n = heights[0].length;
boolean[][] pacific = new boolean[m][n];
boolean[][] atlantic = new boolean[m][n];
for (int i = 0; i < m; i++) {
bfs(i, 0, pacific);
}
for (int j = 1; j < n; j++) {
bfs(0, j, pacific);
}
for (int i = 0; i < m; i++) {
bfs(i, n - 1, atlantic);
}
for (int j = 0; j < n - 1; j++) {
bfs(m - 1, j, atlantic);
}
List<List<Integer>> result = new ArrayList<List<Integer>>();
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (pacific[i][j] && atlantic[i][j]) {
List<Integer> cell = new ArrayList<Integer>();
cell.add(i);
cell.add(j);
result.add(cell);
}
}
}
return result;
}
public void bfs(int row, int col, boolean[][] ocean) {
if (ocean[row][col]) {
return;
}
ocean[row][col] = true;
Queue<int[]> queue = new ArrayDeque<int[]>();
queue.offer(new int[]{row, col});
while (!queue.isEmpty()) {
int[] cell = queue.poll();
for (int[] dir : dirs) {
int newRow = cell[0] + dir[0], newCol = cell[1] + dir[1];
if (newRow >= 0 && newRow < m && newCol >= 0 && newCol < n && heights[newRow][newCol] >= heights[cell[0]][cell[1]] && !ocean[newRow][newCol]) {
ocean[newRow][newCol] = true;
queue.offer(new int[]{newRow, newCol});
}
}
}
}
}