问题:
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:
Given the following 5x5 matrix: Pacific ~ ~ ~ ~ ~ ~ 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 * * * * * * AtlanticReturn:[[0, 4], [1, 3], [1, 4], [2, 2], [3, 0], [3, 1], [4, 0]] (positions with parentheses in above matrix).
解决:
① 水可以向上下左右四个方向流,但是只能向高度小于等于当前高度的方向流,返回同时被两种海水经过的点的坐标。
1、分别处理每个海洋,从海洋边缘(每个海洋两条边)开始,一步步的搜索,即从连接海洋的地方开始搜索,哪些节点的高度大于等于自身(反过来就是可以从那里流到自己),如果是,就标记为true,就这样不断搜索下去。最后所有标记为true的位置就是可以流到对应的海洋。
2、找这两个矩阵,同为true的输出,就是都能流到两个海洋的位置
dfs解决:
class Solution { //22ms
public List<int[]> pacificAtlantic(int[][] matrix) { List<int[]> res = new ArrayList<>(); if (matrix == null || matrix.length == 0 || matrix[0].length == 0) return res; int m = matrix.length; int n = matrix[0].length; boolean[][] pacific = new boolean[m][n]; boolean[][] atlantic = new boolean[m][n]; //递归遍历每个位置 for (int i = 0;i < m;i ++){//由第一列和最后一列开始递归遍历 dfs(matrix,i,0,pacific,Integer.MIN_VALUE); dfs(matrix,i,n - 1,atlantic,Integer.MIN_VALUE); } for (int j = 0;j < n;j ++){//由第一行和最后一行开始递归遍历 dfs(matrix,0,j,pacific,Integer.MIN_VALUE); dfs(matrix,m - 1,j,atlantic,Integer.MIN_VALUE); } //查找两个海洋的交集 for (int i = 0;i < m;i ++){ for (int j = 0;j < n;j ++){ if (pacific[i][j] && atlantic[i][j]){ res.add(new int[]{i,j}); } } } return res; } public void dfs(int[][] matrix,int i,int j,boolean[][] isvisited,int pre){ int m = matrix.length; int n = matrix[0].length; if (i < 0 || i >= m || j < 0 || j >= n || isvisited[i][j] || matrix[i][j] < pre){ return; } isvisited[i][j] = true; dfs(matrix,i + 1,j,isvisited,matrix[i][j]); dfs(matrix,i - 1,j,isvisited,matrix[i][j]); dfs(matrix,i,j + 1,isvisited,matrix[i][j]); dfs(matrix,i,j - 1,isvisited,matrix[i][j]); } }② bfs解决:
class Solution { //31ms
public List<int[]> pacificAtlantic(int[][] matrix) { List<int[]> res = new ArrayList<>(); if (matrix == null || matrix.length == 0 || matrix[0].length == 0) return res; int m = matrix.length; int n = matrix[0].length; boolean[][] pacific = new boolean[m][n]; boolean[][] atlantic = new boolean[m][n]; Queue<int[]> q1 = new LinkedList<>(); Queue<int[]> q2 = new LinkedList<>(); for (int i = 0;i < m;i ++){ q1.offer(new int[]{i,0}); q2.offer(new int[]{i,n - 1}); pacific[i][0] = true; atlantic[i][n - 1] = true; } for (int j = 0;j < n;j ++){ q1.offer(new int[]{0,j}); q2.offer(new int[]{m - 1,j}); pacific[0][j] = true; atlantic[m - 1][j] = true; } bfs(matrix,pacific,q1); bfs(matrix,atlantic,q2); for (int i = 0;i < m;i ++){ for (int j = 0;j < n;j ++){ if (pacific[i][j] && atlantic[i][j]){ res.add(new int[]{i,j}); } } } return res; } public void bfs(int[][] matrix,boolean[][] isvisited,Queue<int[]> q){ int m = matrix.length; int n = matrix[0].length; int[][] dirs = { {0,-1},{-1,0},{0,1},{1,0}}; while(! q.isEmpty()){ int[] tmp = q.poll(); for (int[] dir : dirs){ int x = tmp[0] + dir[0]; int y = tmp[1] + dir[1]; if (x < 0 || x >= m || y < 0 || y >= n || isvisited[x][y] || matrix[x][y] < matrix[tmp[0]][tmp[1]]){ continue; } isvisited[x][y] = true; q.offer(new int[]{x,y}); } } } }