题意

题目

思路

一开始想用双向广搜来做,找他们相碰的点,但是发现对其的理解还是不够完全,导致没写成功。不过,后来想清楚了,之前的错误可能在于从边界点进行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. 关于new Handler()与new Handler(Looper.getMainLooper())区别

    如果你不带参数的实例化:Handler handler=new Handler();那么这个会默认用当前线程的Looper对象. 一般而言,如果你的Handler是要用来刷新UI的,那么就需要在主线程 ...

  2. MySQL字符集 GBK、GB2312、UTF8区别 解决 MYSQL中文乱码问题 收藏 MySQL中涉及的几个字符集

    MySQL中涉及的几个字符集 character-set-server/default-character-set:服务器字符集,默认情况下所采用的.character-set-database:数据 ...

  3. iptables NAT规则【转】

    nat表需要的三个链: 1.PREROUTING:可以在这里定义进行目的NAT的规则,因为路由器进行路由时只检查数据包的目的ip地址,所以为了使数据包得以正确路由,我们必须在路由之前就进行目的NAT; ...

  4. linux文件管理 -> 系统目录结构

    几乎所有的计算机操作系统都是用目录结构组织文件.具体来说就是在一个目录中存放子目录和文件, 而在子目录中又会进一步存放子目录和文件,以此类推形成一个树状的文件结构,由于其结构很像一棵树的分支, 所以该 ...

  5. HTML5学习--SVG全攻略(基础篇)

    明天高级篇 一.什么是SVG? SVG 指的是可伸缩矢量图形 (Scalable Vector Graphics),它用来定义用于网络的基于矢量的图形,使用 XML 格式定义图形.SVG 图像在放大或 ...

  6. 【C语言学习笔记】字符串拼接的3种方法 .

    昨天晚上和@buptpatriot讨论函数返回指针(malloc生成的)的问题,提到字符串拼接,做个总结. #include<stdio.h> #include<stdlib.h&g ...

  7. cbow&&skipgram详细

    前面:关于层次huffman树和负例采样也要知道的,这里就不详细写了 来源于:https://mp.weixin.qq.com/s?__biz=MzI4MDYzNzg4Mw==&mid=224 ...

  8. html元素分类

    目录 块级元素 内联元素 内联块级元素  总结 一.块级元素(block) 定义:默认情况下,独占一行的元素就是块级元素. 特点: 每个块级元素都从新的一行开始,并且其后的元素也另起一行.(真霸道,一 ...

  9. opencv(5)GUI

    OpenCV的图形用户界面(Graphical User Interface, GUI)和绘图等相关功能也是很有用的功能,无论是可视化,图像调试还是我们这节要实现的标注任务,都可以有所帮助. 窗口循环 ...

  10. LanguageTag

    LanguageTag */--> div.org-src-container { font-size: 85%; font-family: monospace; } pre.src { bac ...