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

这道题给了我们一个二维数组,说是数组的左边和上边是太平洋,右边和下边是大西洋,假设水能从高处向低处流,问我们所有能流向两大洋的点的集合。刚开始博主没有理解题意,以为加括号的点是一条路径,连通两大洋的,但是看来看去感觉也不太对,后来终于明白了,是每一个点单独都有路径来通向两大洋。那么就是典型的搜索问题,我最开始想的是对于每个点都来搜索是否能到达边缘,只不过搜索的目标点不再是一个单点,而是所有的边缘点,照这种思路写出的代码无法通过 OJ 大数据集,那么就要想办法来优化代码,优化的方法跟之前那道 Surrounded Regions 很类似,都是换一个方向考虑问题,既然从每个点向中间扩散会 TLE,那么我们就把所有边缘点当作起点开始遍历搜索,然后标记能到达的点为 true,分别标记出 pacific 和 atlantic 能到达的点,那么最终能返回的点就是二者均为 true 的点。我们可以先用DFS来遍历二维数组,参见代码如下:

解法一:

class Solution {
public:
vector<pair<int, int>> pacificAtlantic(vector<vector<int>>& matrix) {
if (matrix.empty() || matrix[].empty()) return {};
vector<pair<int, int>> res;
int m = matrix.size(), n = matrix[].size();
vector<vector<bool>> pacific(m, vector<bool>(n, false));
vector<vector<bool>> atlantic(m, vector<bool>(n, false));
for (int i = ; i < m; ++i) {
dfs(matrix, pacific, INT_MIN, i, );
dfs(matrix, atlantic, INT_MIN, i, n - );
}
for (int i = ; i < n; ++i) {
dfs(matrix, pacific, INT_MIN, , i);
dfs(matrix, atlantic, INT_MIN, m - , i);
}
for (int i = ; i < m; ++i) {
for (int j = ; j < n; ++j) {
if (pacific[i][j] && atlantic[i][j]) {
res.push_back({i, j});
}
}
}
return res;
}
void dfs(vector<vector<int>>& matrix, vector<vector<bool>>& visited, int pre, int i, int j) {
int m = matrix.size(), n = matrix[].size();
if (i < || i >= m || j < || j >= n || visited[i][j] || matrix[i][j] < pre) return;
visited[i][j] = true;
dfs(matrix, visited, matrix[i][j], i + , j);
dfs(matrix, visited, matrix[i][j], i - , j);
dfs(matrix, visited, matrix[i][j], i, j + );
dfs(matrix, visited, matrix[i][j], i, j - );
}
};

那么 BFS 的解法也可以做,用 queue 来辅助,开始把边上的点分别存入 queue 中,然后对应的 map 标记 true,然后开始 BFS 遍历,遍历结束后还是找 pacific 和 atlantic 均标记为 true 的点加入结果 res 中返回即可,参见代码如下:

解法二:

class Solution {
public:
vector<pair<int, int>> pacificAtlantic(vector<vector<int>>& matrix) {
if (matrix.empty() || matrix[].empty()) return {};
vector<pair<int, int>> res;
int m = matrix.size(), n = matrix[].size();
queue<pair<int, int>> q1, q2;
vector<vector<bool>> pacific(m, vector<bool>(n, false)), atlantic = pacific;
for (int i = ; i < m; ++i) {
q1.push({i, });
q2.push({i, n - });
pacific[i][] = true;
atlantic[i][n - ] = true;
}
for (int i = ; i < n; ++i) {
q1.push({, i});
q2.push({m - , i});
pacific[][i] = true;
atlantic[m - ][i] = true;
}
bfs(matrix, pacific, q1);
bfs(matrix, atlantic, q2);
for (int i = ; i < m; ++i) {
for (int j = ; j < n; ++j) {
if (pacific[i][j] && atlantic[i][j]) {
res.push_back({i, j});
}
}
}
return res;
}
void bfs(vector<vector<int>>& matrix, vector<vector<bool>>& visited, queue<pair<int, int>>& q) {
int m = matrix.size(), n = matrix[].size();
vector<vector<int>> dirs{{,-},{-,},{,},{,}};
while (!q.empty()) {
auto t = q.front(); q.pop();
for (auto dir : dirs) {
int x = t.first + dir[], y = t.second + dir[];
if (x < || x >= m || y < || y >= n || visited[x][y] || matrix[x][y] < matrix[t.first][t.second]) continue;
visited[x][y] = true;
q.push({x, y});
}
}
}
};

Github 同步地址:

https://github.com/grandyang/leetcode/issues/417

参考资料:

https://leetcode.com/problems/pacific-atlantic-water-flow/

https://leetcode.com/problems/pacific-atlantic-water-flow/discuss/90733/java-bfs-dfs-from-ocean

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Pacific Atlantic Water Flow 太平洋大西洋水流的更多相关文章

  1. [LeetCode] 417. Pacific Atlantic Water Flow 太平洋大西洋水流

    Given an m x n matrix of non-negative integers representing the height of each unit cell in a contin ...

  2. 417 Pacific Atlantic Water Flow 太平洋大西洋水流

    详见:https://leetcode.com/problems/pacific-atlantic-water-flow/description/ C++: class Solution { publ ...

  3. Leetcode: Pacific Atlantic Water Flow

    Given an m x n matrix of non-negative integers representing the height of each unit cell in a contin ...

  4. [LeetCode] Pacific Atlantic Water Flow 题解

    题意 题目 思路 一开始想用双向广搜来做,找他们相碰的点,但是发现对其的理解还是不够完全,导致没写成功.不过,后来想清楚了,之前的错误可能在于从边界点进行BFS,其访问顺序应该是找到下一个比当前那个要 ...

  5. [Swift]LeetCode417. 太平洋大西洋水流问题 | Pacific Atlantic Water Flow

    Given an m x n matrix of non-negative integers representing the height of each unit cell in a contin ...

  6. 【LeetCode】417. Pacific Atlantic Water Flow 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/pacific- ...

  7. LeetCode 417. Pacific Atlantic Water Flow

    原题链接在这里:https://leetcode.com/problems/pacific-atlantic-water-flow/description/ 题目: Given an m x n ma ...

  8. 417. Pacific Atlantic Water Flow

    正常做的,用了645MS..感觉DFS的时候剪枝有问题.. 为了剪枝可能需要标记一个点的4种情况: 1:滨临大西洋,所有太平洋来的点可以通过: 2:濒临太平洋,所有大西洋来的点可以通过: 3:都不濒临 ...

  9. Leetcode 417.太平洋大西洋水流问题

    太平洋大西洋水流问题 给定一个 m x n 的非负整数矩阵来表示一片大陆上各个单元格的高度."太平洋"处于大陆的左边界和上边界,而"大西洋"处于大陆的右边界和下 ...

随机推荐

  1. 读书笔记--SQL必知必会01--了解SQL

    1.1 数据库基础 数据库(datebase) 保存有组织的数据的容器(通常是一个文件或一组文件),是一个以某种有组织的方式存储的数据集合. 数据库管理系统(DBMS,Data Base Manage ...

  2. 【中文分词】二阶隐马尔可夫模型2-HMM

    在前一篇中介绍了用HMM做中文分词,对于未登录词(out-of-vocabulary, OOV)有良好的识别效果,但是缺点也十分明显--对于词典中的(in-vocabulary, IV)词却未能很好地 ...

  3. iFrame 功能详解

    目录索引: 一. 简介二. 属性三. 功能四. 应用 一.简介 网页“帧”的概念最早是由Netscape所提出,当时全部由“帧”构成的页面,也被称之为 “框架集”页面,在一个“框架集”页面中,“帧” ...

  4. ubuntu学习的简单笔记

    l vi编辑器开发步骤 A)输入 vi Hello.java B) 输入 i 插入模式. C)输入 冒号.[保存退出:wq][退出不保存:q!] l 列出当前目录的所有文件:ls 详细信息的列表:ls ...

  5. C#开发微信门户及应用(5)--用户分组信息管理

    在上个月的对C#开发微信门户及应用做了介绍,写过了几篇的随笔进行分享,由于时间关系,间隔了一段时间没有继续写这个系列的博客了,并不是对这个方面停止了研究,而是继续深入探索这方面的技术,为了更好的应用起 ...

  6. 【项目管理】图解GitHub基本操作

    一.注册并登陆到github网站 1.1.打开github网站首页(https://github.com/) 1.2.注册一个自己的github账号 创建账户后再验证自己的邮箱,然后就可以登陆到git ...

  7. 一个成功的BI项目实施需要注意哪些?

    BI是所有IT系统中最适合于管理层和决策层使用的信息系统. ERP等各类信息系统中的数据,只有通过BI才能将隐藏在数据中的信息挖掘出来.转化为事实. BI的实施也需要依据企业情况进行“定制”.如何实施 ...

  8. iOS 开发者账号到期续费流程

    1.登录developer.apple.com,查看到期时间 2.到期提醒通知,点击Renew Membership续费(一般提前一个月提醒续费) 3.个人开发者账号续费需要支付 688人民币/年(9 ...

  9. 组件RecyclerView的应用(一)

    首先我们知道RecyclerView组件是ListView的升级版,今天先介绍基础的RecyclerView的基本布局RecyclerView.Adapter和LayoutManager 第一: La ...

  10. C# 知识特性 Attribute

    C#知识--获取特性 Attribute 特性提供功能强大的方法,用以将元数据或声明信息与代码(程序集.类型.方法.属性等)相关联.特性与程序实体关联后,可在运行时使用"反射"查询 ...