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

     解法1:DFS,遍历每一个元素,找到路径,复杂度过高,有些case超时

    class Solution {
    public:
        vector<vector<int>> pacificAtlantic(vector<vector<int>>& matrix) {
            vector<vector<int>>res;
            int m = matrix.size();
            if (m < 1)return res;
            int n = matrix[0].size();
            if (n < 1)return res;
            
            for (int i = 0; i < m; ++i)
            {
                for(int j = 0; j < n; ++j)
                {
                    bool f1 = false, f2 = false;
                    vector<vector<bool>> marked(m, vector<bool>(n, false));
                    
                    dfs(matrix, i, j, f1, f2, marked);
                    
                    if (f1 && f2)res.push_back(vector<int>{i,j});
                }
            }
            return res;
        }
        
        void dfs(vector<vector<int>>& matrix, int i, int j, bool& flag1, bool& flag2, vector<vector<bool>>& marked)
        {
            if ((flag1 && flag2) || marked[i][j])return;
            int dir[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};
            marked[i][j] = true;
            for (auto d: dir)
            {
                if (i+d[0] < 0 || j+d[1] < 0) // 当前格子已经第一排或第一列了
                {
                    flag1 = true;
                    continue;
                }
                if (i+d[0] >= matrix.size() || j+d[1] >= matrix[0].size()) // 当前格子已经最后一排或者最后一列了
                {
                    flag2 = true;
                    continue;
                }
                // 下一个方格比当前的低,并且之前没有走过
                if(matrix[i+d[0]][j+d[1]] <= matrix[i][j] && !marked[i+d[0]][j+d[1]])
                    dfs(matrix, i+d[0], j+d[1], flag1, flag2, marked);
                if (flag1 && flag2)return;
            }
        }
    };

    解法2:DFS从太平洋和大西洋往内部遍历

    class Solution {
    public:
        vector<vector<int>> pacificAtlantic(vector<vector<int>>& matrix) {
            vector<vector<int>>res;
            int m = matrix.size();
            if (m < 1)return res;
            int n = matrix[0].size();
            if (n < 1)return res;
            vector<vector<bool>> marked1(m, vector<bool>(n, false));
            vector<vector<bool>> marked2(m, vector<bool>(n, false));
            for (int i = 0;i < m; ++i)
            {
                dfs(matrix, i, 0, marked1);
                dfs(matrix, i, n-1, marked2);
            }
            for (int i = 0;i < n;++i)
            {
                dfs(matrix, 0, i, marked1);
                dfs(matrix, m-1, i, marked2);
            }
            
            for (int i = 0; i < m; ++i)
            {
                for(int j = 0; j < n; ++j)
                {
                    if (marked1[i][j] && marked2[i][j])res.push_back(vector<int>{i,j});
                }
            }
            return res;
        }
        
        void dfs(vector<vector<int>>& matrix, int i, int j, vector<vector<bool>>& marked)
        {
            if (marked[i][j])return; 
            int dir[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};
            marked[i][j] = true;
            for (auto d: dir)
            {
                int i_next = i + d[0];
                int j_next = j + d[1];
    
                if(i_next < 0 || i_next >= matrix.size() || j_next < 0 || j_next >= matrix[0].size() || 
                  matrix[i][j] > matrix[i_next][j_next])
                    continue;
                dfs(matrix, i+d[0], j+d[1], marked);
            }
        }
    };
  • 相关阅读:
    当前页面跳转网页,新打开和在本页面打开
    文本框内按下enter键禁止换行
    排序算法
    Java实现图片的叠加与拼接
    简单工厂模式
    单例模式
    Java实现代理模式
    调用第三方接口生成短链接(五)
    调用第三方接口生成短链接(四)
    调用第三方接口生成短链接(二)
  • 原文地址:https://www.cnblogs.com/qiang-wei/p/12359484.html
Copyright © 2020-2023  润新知