题意

题目

思路

一开始想用双向广搜来做,找他们相碰的点,但是发现对其的理解还是不够完全,导致没写成功。不过,后来想清楚了,之前的错误可能在于从边界点进行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. [Ubuntu 14.04] 安装Flash && 安装QQ

    一.安装Flash 打开Firefox浏览器弹出的Flash安装提醒早都烦死了,那么Ubuntu14.04怎么安装Flash呢? 1.32位系统命令行安装: 第一步 更新库: sudo apt-get ...

  2. Add Two Numbers I & II

    Add Two Numbers I You have two numbers represented by a linked list, where each node contains a sing ...

  3. 理解mipi协议【转】

    转自:http://blog.csdn.net/wanglining1987/article/details/50202615 完成mipi信号通道分配后,需要生成与物理层对接的时序.同步信号: MI ...

  4. XSS注入常用语句

    <script>alert('hello,gaga!');</script> //经典语句,哈哈! >"'><img src="javas ...

  5. python目前安装的包备份

    Package Version ------------------------------- ------------------ alembic altgraph 0.14 apistar app ...

  6. RobotFramework基本用法(二)

    双击打开C:\Python27\Scripts目录下的 ride.py 一,定义变量,打印 1,右键File-->New Poreject,在项目下右键New suite,在套件下右键 New ...

  7. npm 下载node-zookeeper包

    环境:centos7(lunix) 1.安装nvm curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.6/install. ...

  8. 09 Command Documentation 命令文档

    Command Documentation 命令文档   There is a suite of programs to build and process Go source code. Inste ...

  9. 试用Redis

    Windows 10家庭中文版,运行于VirtualBox上的Ubuntu 18.04,Redis 4.0.10, Redis,久仰大名!因为没有从事互联网行业,所以一直没有使用过.近期找工作,也隐约 ...

  10. Scrapy:创建爬虫程序的方式

    Windows 10家庭中文版,Python 3.6.4,Scrapy 1.5.0, 在Scrapy中,建立爬虫程序或项目的方式有两种(在孤读过Scrapy的大部分文档后): 1.继承官方Spider ...