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 *
* * * * * Atlantic
Return:
[[0, 4], [1, 3], [1, 4], [2, 2], [3, 0], [3, 1], [4, 0]] (positions with parentheses in above matrix).
思路:BFS或者DFS,目测DFS慢一点
package l417;
import java.util.ArrayList;
import java.util.List;
// 标准DFS + 2个全局boolean
public class dfs_solution {
List<int[]> rst = new ArrayList<int[]>();
int[] dx = new int[]{0,0,1,-1};
int[] dy = new int[]{1,-1,0,0};
boolean[][] marked, canReach;
boolean Pacific = false, Atlantic = false;
public List<int[]> pacificAtlantic(int[][] matrix) {
if(matrix.length == 0) return rst;
int rows = matrix.length, cols = matrix[0].length;
marked = new boolean[rows][cols]; canReach = new boolean[rows][cols];
for(int i=0; i<rows; i++)
for(int j=0; j<cols; j++) {
Pacific = false; Atlantic = false;
marked[i][j] = true;
dfs(matrix, i, j);
marked[i][j] = false;
if(Pacific && Atlantic) {
canReach[i][j] = true;
rst.add(new int[]{i, j});
}
}
return rst;
}
private void dfs(int[][] matrix, int i, int j) {
if(i == 0 || j == 0) Pacific = true;
if(i == matrix.length-1 || j == matrix[0].length-1) Atlantic = true;
if(canReach[i][j]) {Pacific=true; Atlantic=true;}
if(Pacific && Atlantic) return;
for(int k=0; k<4; k++) {
int newi = i+dx[k], newj = j+dy[k];
if(ok(matrix, newi, newj) && !marked[newi][newj] && matrix[i][j]>=matrix[newi][newj]) {
marked[newi][newj] = true;
dfs(matrix, newi, newj);
marked[newi][newj] = false;
}
}
}
private boolean ok(int[][] matrix, int i, int j) {
if(i<0 || i>=matrix.length || j<0 || j>=matrix[0].length) return false;
return true;
}
}
package l417;
import java.util.*;
public class Solution {
public List<int[]> pacificAtlantic(int[][] matrix) {
List<int[]> rst = new ArrayList<int[]>();
if(matrix.length == 0) return rst;
int rows = matrix.length, cols = matrix[0].length;
boolean[][] Pacific = new boolean[rows][cols], Atlantic = new boolean[rows][cols];
int[] dx = new int[]{0,0,1,-1};
int[] dy = new int[]{1,-1,0,0};
Queue<int[]> cur = new LinkedList<int[]>();
Queue<int[]> next = new LinkedList<int[]>();
// pacific
for(int i=0; i<rows; i++) {
Pacific[i][0] = true;
cur.add(new int[]{i, 0});
}
for(int i=0; i<cols; i++) {
Pacific[0][i] = true;
cur.add(new int[]{0, i});
}
while(!cur.isEmpty()) {
int[] r = cur.remove();
for(int k=0; k<4; k++) {
int newi = r[0]+dx[k], newj = r[1]+dy[k];
if(ok(matrix, newi, newj) && !Pacific[newi][newj] && matrix[r[0]][r[1]]<=matrix[newi][newj]) {
next.add(new int[]{newi, newj});
Pacific[newi][newj] = true;
}
}
if(cur.isEmpty()) {
cur = next;
next = new LinkedList<int[]>();
}
}
// atlantic
for(int i=0; i<rows; i++) {
Atlantic[i][cols-1] = true;
cur.add(new int[]{i, cols-1});
}
for(int i=0; i<cols; i++) {
Atlantic[rows-1][i] = true;
cur.add(new int[]{rows-1, i});
}
while(!cur.isEmpty()) {
int[] r = cur.remove();
for(int k=0; k<4; k++) {
int newi = r[0]+dx[k], newj = r[1]+dy[k];
if(ok(matrix, newi, newj) && !Atlantic[newi][newj] && matrix[r[0]][r[1]]<=matrix[newi][newj]) {
next.add(new int[]{newi, newj});
Atlantic[newi][newj] = true;
}
}
if(cur.isEmpty()) {
cur = next;
next = new LinkedList<int[]>();
}
}
for(int i=0; i<rows;i++)
for(int j=0; j<cols; j++)
if(Pacific[i][j] && Atlantic[i][j])
rst.add(new int[]{i, j});
return rst;
}
private boolean ok(int[][] matrix, int i, int j) {
if(i<0 || i>=matrix.length || j<0 || j>=matrix[0].length) return false;
return true;
}
}
但是为什么BFS还只击败了16%??还有哪里可以优化??
本文探讨了一种求解特定矩阵问题的算法——太平洋大西洋水流遍历问题,通过BFS和DFS两种方法实现了从矩阵边界开始寻找能够同时流向两个海洋的网格坐标。对比两种方法并讨论了BFS实现效率的问题。
547

被折叠的 条评论
为什么被折叠?



