zoukankan      html  css  js  c++  java
  • 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);
            }
        }
    };
  • 相关阅读:
    IOS之推送通知(本地推送和远程推送)
    IOS,苹果内购和添加广告
    CSS3选择器、背景、边框、文本
    CSS2D旋转、过渡、动画
    JavaScript Array、Date、String
    那些不常用却很有的CSS
    纯CSS打造兼容各种浏览器的几何图形
    安装 SQLManagementStudio_x86_CHS(SQL Server Management Studio) 老提示重启的解决办法
    关于使用Html.RenderPartial和Html.Partial显示分部视图时提示参数错误的BUG
    学习从实践开始之jQuery插件开发:对话框插件开发
  • 原文地址:https://www.cnblogs.com/qiang-wei/p/12359484.html
Copyright © 2011-2022 走看看