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:

  1. The order of returned grid coordinates does not matter.
  2. 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).

思路:用两个boolean矩阵来表示,水往里面走,往高的地势走,看能流向哪里,这样可以减少计算;同时最后两个矩阵重合的地方,就是overlap point which can flow to both side;

class Solution { 

    public List<List<Integer>> pacificAtlantic(int[][] matrix) {
        List<List<Integer>> lists = new ArrayList<List<Integer>>();
        if(matrix == null || matrix.length == 0 || matrix[0].length == 0) {
            return lists;
        }
        int n = matrix.length;
        int m = matrix[0].length;
        
        boolean[][] visitp = new boolean[n][m];
        boolean[][] visita = new boolean[n][m];
        
        Queue<int[]> pqueue = new LinkedList<int[]>();
        Queue<int[]> aqueue = new LinkedList<int[]>();
        
        for(int i = 0; i < n; i++) {
            for(int j = 0; j < m; j++) {
                if(i == 0 || j == 0) {
                    pqueue.offer(new int[]{i, j});
                    visitp[i][j] = true;
                }
                if(i == n - 1 || j == m - 1) {
                    aqueue.offer(new int[]{i, j});
                    visita[i][j] = true;
                }
            }
        }
        
        bfs(pqueue, matrix, visitp);
        bfs(aqueue, matrix, visita);
        
        for(int i = 0; i < n; i++) {
            for(int j = 0; j < m; j++) {
                if(visitp[i][j] && visita[i][j]) {
                    List<Integer> list = new ArrayList<Integer>();
                    list.add(i);
                    list.add(j);
                    lists.add(list);   
                }
            }
        }
        return lists;
    }
    
    private void bfs(Queue<int[]> queue, int[][] matrix, boolean[][] visited) {
        int[] dx = {0,0,-1,1};
        int[] dy = {-1,1,0,0};
        int n = matrix.length;
        int m = matrix[0].length;
        
        while(!queue.isEmpty()) {
            int[] head = queue.poll();
            int headx = head[0];
            int heady = head[1];
            visited[headx][heady] = true;
            for(int k = 0; k < 4; k++) {
                int nx = headx + dx[k];
                int ny = heady + dy[k];
                if(0 <= nx && nx < n && 0 <= ny && ny < m 
                  && !visited[nx][ny] && matrix[nx][ny] >= matrix[headx][heady]) {
                    visited[nx][ny] = true;
                    queue.offer(new int[]{nx, ny});
                }
            }
        }
    }
}
发布了710 篇原创文章 · 获赞 13 · 访问量 19万+

猜你喜欢

转载自blog.csdn.net/u013325815/article/details/105307098