417 Pacific Atlantic Water Flow 太平洋大西洋水流
详见:https://leetcode.com/problems/pacific-atlantic-water-flow/description/
C++:
class Solution {
public:
vector<pair<int, int>> pacificAtlantic(vector<vector<int>>& matrix) {
if (matrix.empty() || matrix[0].empty())
{
return {};
}
vector<pair<int, int>> res;
int m = matrix.size(), n = matrix[0].size();
vector<vector<bool>> pacific(m, vector<bool>(n, false));
vector<vector<bool>> atlantic(m, vector<bool>(n, false));
for (int i = 0; i < m; ++i)
{
dfs(matrix, pacific, INT_MIN, i, 0);
dfs(matrix, atlantic, INT_MIN, i, n - 1);
}
for (int i = 0; i < n; ++i)
{
dfs(matrix, pacific, INT_MIN, 0, i);
dfs(matrix, atlantic, INT_MIN, m - 1, i);
}
for (int i = 0; i < m; ++i)
{
for (int j = 0; 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[0].size();
if (i < 0 || i >= m || j < 0 || j >= n || visited[i][j] || matrix[i][j] < pre)
{
return;
}
visited[i][j] = true;
dfs(matrix, visited, matrix[i][j], i + 1, j);
dfs(matrix, visited, matrix[i][j], i - 1, j);
dfs(matrix, visited, matrix[i][j], i, j + 1);
dfs(matrix, visited, matrix[i][j], i, j - 1);
}
};
参考:https://www.cnblogs.com/grandyang/p/5962508.html
417 Pacific Atlantic Water Flow 太平洋大西洋水流的更多相关文章
- [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 ...
- [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 ...
- 【LeetCode】417. Pacific Atlantic Water Flow 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/pacific- ...
- LeetCode 417. Pacific Atlantic Water Flow
原题链接在这里:https://leetcode.com/problems/pacific-atlantic-water-flow/description/ 题目: Given an m x n ma ...
- 417. Pacific Atlantic Water Flow
正常做的,用了645MS..感觉DFS的时候剪枝有问题.. 为了剪枝可能需要标记一个点的4种情况: 1:滨临大西洋,所有太平洋来的点可以通过: 2:濒临太平洋,所有大西洋来的点可以通过: 3:都不濒临 ...
- [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 ...
- 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 ...
- [LeetCode] Pacific Atlantic Water Flow 题解
题意 题目 思路 一开始想用双向广搜来做,找他们相碰的点,但是发现对其的理解还是不够完全,导致没写成功.不过,后来想清楚了,之前的错误可能在于从边界点进行BFS,其访问顺序应该是找到下一个比当前那个要 ...
- Java实现 LeetCode 417 太平洋大西洋水流问题
417. 太平洋大西洋水流问题 给定一个 m x n 的非负整数矩阵来表示一片大陆上各个单元格的高度."太平洋"处于大陆的左边界和上边界,而"大西洋"处于大陆的 ...
随机推荐
- XP 系统如何安装.NET Framework4.0
1 运行CMD,然后输入命令net stop WuAuServ 2 打开C盘的Windows目录,然后找到SoftwareDistribution文件夹改名为SDold. 3 在CMD窗口中输 ...
- android 多进程 Binder AIDL Service
本文參考http://blog.csdn.net/saintswordsman/article/details/5130947 android的多进程是通过Binder来实现的,一个类,继承了Bind ...
- java SE基础(Collection接口概述)
Collection接口相关集成关系例如以下图 1. 关于可迭代接口(Iterable) 可迭代接口仅包括一个方法,返回一个在一组T类型元素上进行迭代的迭代器: public ...
- soapUI系列之—-02 Groovy脚本常用方法
------Groovy脚本常用方法 1. 设置参数值:setPropertyValuea. 设置 project level property//set to project level prope ...
- Redis入门教程(三)— Java中操作Redis
在Redis的官网上,我们可以看到Redis的Java客户端众多 其中,Jedis是Redis官方推荐,也是使用用户最多的Java客户端. 开始前的准备 使用jedis使用到的jedis-2.1.0. ...
- 通过GhostDoc实现自定义方法概要(summary)
首先是下载GhostDoc 来自园友:http://www.cnblogs.com/VAllen/p/GhostDocPro49.html 修改模板 安装好后,修改下模板,工具>GhostDoc ...
- WWDC笔记:2013 Session 203 What’s New in Cocoa Touch(未完)
Multitasking Background fetching New background mode fetch - (void)application:(UIApplication *)appl ...
- MySQL服务无法启动(1067)问题
关于这个问题网上的帖子和说法多如牛毛,是在难以分辨真假,或者是否与自己的出错情况相同. 有了前车之鉴,就有必要提前声明,这篇是我在计算机--管理--服务中启动mysql服务时出现的错误,如下: 最后的 ...
- wget和curl
1 curl比wget支持更多的协议 2 wget是支持递归的,而curl不支持
- es的forcemerge——按照天分割
归并线程配置 segment 归并的过程,需要先读取 segment,归并计算,再写一遍 segment,最后还要保证刷到磁盘.可以说,这是一个非常消耗磁盘 IO 和 CPU 的任务.所以,ES 提供 ...