题意

题目

思路

一开始想用双向广搜来做,找他们相碰的点,但是发现对其的理解还是不够完全,导致没写成功。不过,后来想清楚了,之前的错误可能在于从边界点进行BFS,其访问顺序应该是找到下一个比当前那个要大的点,但是我写反了。。可以先对左边的队列进行BFS,保存其visited,再接着对右边的队列进行BFS,当访问到之前已经访问过的结点时,则加入到结果中。

实现

//
// #include "../PreLoad.h" /*
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). */ class Solution {
public:
/**
* 从边界的点开始进行BFS,找出交叉的点,即为可以到达两边的点
* @param matrix
* @return
*/
vector<pair<int, int>> pacificAtlantic(vector<vector<int>>& matrix) {
vector<pair<int, int>> result;
if (matrix.size() == 0) {
return result;
} // 双向BFS,找重合点
queue<pair<int, int>> pa_queue; //太平洋
queue<pair<int, int>> at_queue; //大西洋 size_t row = matrix.size();
size_t col = matrix[0].size(); vector<vector<int>> layouts = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
auto state_bfs = [&](queue<pair<int, int>>& queue, vector<vector<int>>& visit) {
while (!queue.empty()) {
pair<int, int> content = queue.front();
queue.pop(); int height = content.first;
int x = content.second / col;
int y = content.second % col; for (auto temp : layouts) {
int newx = temp[0] + x;
int newy = temp[1] + y; // 因为是从边界结点开始,题目又要求某个点从高往低到两个海洋,所以边界点访问顺序则为递增
if (newx < 0 || newx >= row || newy < 0 || newy >= col || visit[newx][newy] || matrix[newx][newy] < height) {
continue;
} queue.push({matrix[newx][newy], newx * col + newy});
visit[newx][newy] = 1;
}
}
}; vector<vector<int>> visited1(row, vector<int>(col, 0));
vector<vector<int>> visited2(row, vector<int>(col, 0)); // 第一行和第一列
for (int i = 0; i < col; i++) {
pa_queue.push({matrix[0][i], i});
visited1[0][i] = 1; at_queue.push({matrix[row-1][i], (row-1) * col + i});
visited2[row-1][i] = 1;
}
for (int i = 0; i < row; i++) {
pa_queue.push({matrix[i][0], i * col});
visited1[i][0] = 1; at_queue.push({matrix[i][col-1], i * col + col-1});
visited2[i][col-1] = 1;
} state_bfs(pa_queue, visited1);
state_bfs(at_queue, visited2); // 交叉点则为可以到达两边的点
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (visited1[i][j] && visited2[i][j]) {
result.push_back({i, j});
}
}
} return result;
} vector<pair<int, int>> pacificAtlantic2(vector<vector<int>>& matrix) {
vector<pair<int, int>> result;
if (matrix.size() == 0) {
return result;
} // 双向BFS,找重合点
queue<pair<int, int>> pa_queue; //太平洋
queue<pair<int, int>> at_queue; //大西洋 size_t row = matrix.size();
size_t col = matrix[0].size(); vector<vector<int>> layouts = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
auto state_bfs = [&](queue<pair<int, int>>& queue, vector<vector<int>>& visit, bool flag) {
while (!queue.empty()) {
pair<int, int> content = queue.front();
queue.pop(); int height = content.first;
int x = content.second / col;
int y = content.second % col; for (auto temp : layouts) {
int newx = temp[0] + x;
int newy = temp[1] + y; // 因为是从边界结点开始,题目又要求某个点从高往低到两个海洋,所以边界点访问顺序则为递增
if (newx < 0 || newx >= row || newy < 0 || newy >= col || matrix[newx][newy] < height) {
continue;
} if (flag) {
if (visit[newx][newy] != 1) {
// 已经被另外一个访问过
if (visit[newx][newy] == 2) {
result.push_back({newx, newy});
continue;
} queue.push({matrix[newx][newy], newx * col + newy});
visit[newx][newy] = 1;
}
}
else {
if (visit[newx][newy] != 2) {
// 已经被另外一个访问过
if (visit[newx][newy] == 1) {
result.push_back({newx, newy});
continue;
} queue.push({matrix[newx][newy], newx * col + newy});
visit[newx][newy] = 2;
}
}
}
}
}; vector<vector<int>> visited1(row, vector<int>(col, 0));
vector<vector<int>> visited2(row, vector<int>(col, 0)); // 第一行和第一列
for (int i = 0; i < col; i++) {
pa_queue.push({matrix[0][i], i});
visited1[0][i] = 1; at_queue.push({matrix[row-1][i], (row-1) * col + i});
visited2[row-1][i] = 2;
}
for (int i = 0; i < row; i++) {
pa_queue.push({matrix[i][0], i * col});
visited1[i][0] = 1; at_queue.push({matrix[i][col-1], i * col + col-1});
visited2[i][col-1] = 2;
} while (!pa_queue.empty() || !at_queue.empty()) {
if (!pa_queue.empty()) {
state_bfs(pa_queue, visited1, true);
} if (!at_queue.empty()) {
state_bfs(at_queue, visited2, false);
}
} // 交叉点则为可以到达两边的点
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (visited1[i][j] && visited2[i][j]) {
result.push_back({i, j});
}
}
} return result;
} void test() {
vector<vector<int>> matrix = {
{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}
}; vector<pair<int, int>> result = this->pacificAtlantic2(matrix);
for (int i = 0; i < result.size(); i++) {
pair<int, int> content = result[i];
cout << "(" << content.first << ", " << content.second << ")" << endl;
}
}
};

[LeetCode] Pacific Atlantic Water Flow 题解的更多相关文章

  1. [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 ...

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

  3. LeetCode 417. Pacific Atlantic Water Flow

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

  4. [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 ...

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

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

  6. [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 ...

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

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

  8. 417. Pacific Atlantic Water Flow

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

  9. [LeetCode] Trapping Rain Water II 题解

    题意 题目 思路 我一开始想的时候只考虑到一个结点周围的边界的情况,并没有考虑到边界的高度其实影响到所有的结点盛水的高度. 我们可以发现,中间是否能够盛水取决于边界是否足够高于里面的高度,所以这必然是 ...

随机推荐

  1. imperva命令行查看流量值大小

    watch -d -n 1 /proc/hades/status echo clear > /proc/hades/status     //清除这些记录

  2. bind系统调用

    /* * Bind a name to a socket. Nothing much to do here since it's * the protocol's responsibility to ...

  3. 2018 ICPC 徐州网络赛

    2018 ICPC 徐州网络赛 A. Hard to prepare 题目描述:\(n\)个数围成一个环,每个数是\(0\)~\(2^k-1\),相邻两个数的同或值不为零,问方案数. solution ...

  4. EPC摘抄

    S6a MME – HSS 完成用户位置信息的交换和用户签约信息的管理,传送控制面信息 Diameter MME:主要负责信令处理及移动性管理,功能包括:NAS信令及其安全:跟踪区域(Tracking ...

  5. ksh函数

    在不同的shell环境里,shell脚本的写法是不同的 此链接为ksh环境的函数写法: https://blog.csdn.net/shangboerds/article/details/487115 ...

  6. 分布式git

    分布式 Git 你现在拥有了一个远程 Git 版本库,能为所有开发者共享代码提供服务,在一个本地工作流程下,你也已经熟悉 了基本 Git 命令.你现在可以学习如何利用 Git 提供的一些分布式工作流程 ...

  7. js函数前加分号和感叹号是什么意思?有什么用?

    一般看JQuery插件里的写法是这样的 (function($) { //... })(jQuery); 今天看到bootstrap的javascript组件是这样写的 !function( $ ){ ...

  8. js面试题之求数组最值

    今天继续分享js常见的面试题,求数组最大值,最小值,这里列举4种常见解法,还有其他方法也可以实现,读者知道可以私信我,我将把意见列举到博客中,欢迎提出意见. 第一种,利用数组排序 var arr=[3 ...

  9. django(2)基本指令

    打开 Linux 或 MacOS 的 Terminal (终端)直接在 终端中输入这些命令(不是 python 的 shell中) 如果是 windows 用 cmd(开始 搜索 cmd 或者 快捷键 ...

  10. Mysql建表语句

    create table messages( id int unsigned primary key auto_increment, sender varchar(64) not null, gett ...