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);
            }
        }
    };
  • 相关阅读:
    SSM实现mysql数据库账号密码加密连接
    获取系统相关信息 (CPU使用率 内存使用率 系统磁盘大小)
    JavaWeb(一) / /* /**的区别
    IDEA(一) 使用IDEA搭建SSM框架项目
    Mysql连接数据库异常汇总【必收藏】
    Java代理模式及动态代理详解
    SpringBoot集成Thymeleaf
    设计师,程序员,当心字体侵权
    Java开发神器Lombok使用详解
    日期格式化跨年bug,是否与你不期而遇?
  • 原文地址:https://www.cnblogs.com/qiang-wei/p/12359484.html
Copyright © 2011-2022 走看看