zoukankan      html  css  js  c++  java
  • 0417. Pacific Atlantic Water Flow (M)

    Pacific Atlantic Water Flow (M)

    题目

    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).
    

    题意

    给定一个二维数组,数组第一排上方和第一列左侧是太平洋,最后一排下方和最后一列右侧是大西洋,每个元素代表该处水位的高度,每个位置的水只能向低处流动。计算所有既能到达太平洋又能到达大西洋的水的位置。

    思路

    关键是要标记出所有能到达太平洋/大西洋的坐标。以太平洋为例,将第一行及第一列所有位置标记为可到达,利用BFS或DFS将剩余所有可到达的位置标记出来。最后筛选出既可到达太平洋又可到达大西洋的坐标。


    代码实现

    Java

    class Solution {
        public List<List<Integer>> pacificAtlantic(int[][] matrix) {
            if (matrix.length == 0) return new ArrayList<>();
            
            List<List<Integer>> ans = new ArrayList<>();
            int m = matrix.length, n = matrix[0].length;
            boolean[][] toPacific = new boolean[m][n];
            boolean[][] toAtlantic = new boolean[m][n];
    
            judge(matrix, toPacific, 0, 0);
            judge(matrix, toAtlantic, m - 1, n - 1);
    
            for (int i = 0; i < m; i++) {
                for (int j = 0; j < n; j++) {
                    if (toPacific[i][j] && toAtlantic[i][j]) {
                        ans.add(Arrays.asList(i, j));
                    }
                }
            }
    
            return ans;
        }
    
        private void judge(int[][] matrix, boolean[][] toOcean, int row, int col) {
            Queue<int[]> q = new LinkedList<>();
            int m = matrix.length, n = matrix[0].length;
            int[] shiftX = {-1, 1, 0, 0}, shiftY = {0, 0, -1, 1};
    
            for (int i = 0; i < n; i++) {
                toOcean[row][i] = true;
                q.offer(new int[]{row, i});
            }
            for (int i = 0; i < m; i++) {
                toOcean[i][col] = true;
                if (i != row) q.offer(new int[]{i, col});
            }
    
            while (!q.isEmpty()) {
                int[] coord = q.poll();
                int x = coord[0], y = coord[1];
                for (int i = 0; i < 4; i++) {
                    int nextX = x + shiftX[i], nextY = y + shiftY[i];
                    if (isValid(m, n, nextX, nextY) && matrix[x][y] <= matrix[nextX][nextY] &&!toOcean[nextX][nextY]) {
                        toOcean[nextX][nextY] = true;
                        q.offer(new int[]{nextX, nextY});
                    }
                }
            }
        }
    
        private boolean isValid(int m, int n, int i, int j) {
            return i >= 0 && i < m && j >= 0 && j < n;
        }
    }
    
  • 相关阅读:
    [云计算&大数据]概念辨析:数据仓库 | 数据湖 | 数据中心 | 数据中台 | 数据平台 【待续】
    [云计算]概念辨析:云计算 [IaaS/PaaS/SaaS & 公有云/私有云/混合云]
    [Linux]浅析"command > /dev/null 2>&1 &" 与 "command 1>/dev/null 2>&1 &"
    [Python]【Form Data vs Request Payload】之 python 爬虫如何实现 POST request payload 形式的请求
    [Python]PyCharm中出现unresolved reference的解决方法
    [数据库/MYSQL]MYSQL开启Bin-Log
    scrapy采集gb2312网页中文乱码笔记
    uTools 桌面软件。
    Asp.Net Core Grpc 入门实践
    ASP.NET Core 中间件(Middleware)(一)
  • 原文地址:https://www.cnblogs.com/mapoos/p/14578599.html
Copyright © 2011-2022 走看看