417. Pacific Atlantic Water Flow
Posted qiang-wei
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了417. Pacific Atlantic Water Flow相关的知识,希望对你有一定的参考价值。
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:
- The order of returned grid coordinates does not matter.
- 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).
解法1:DFS,遍历每一个元素,找到路径,复杂度过高,有些case超时
class Solution { public: vector<vector<int>> pacificAtlantic(vector<vector<int>>& matrix) { vector<vector<int>>res; int m = matrix.size(); if (m < 1)return res; int n = matrix[0].size(); if (n < 1)return res; for (int i = 0; i < m; ++i) { for(int j = 0; j < n; ++j) { bool f1 = false, f2 = false; vector<vector<bool>> marked(m, vector<bool>(n, false)); dfs(matrix, i, j, f1, f2, marked); if (f1 && f2)res.push_back(vector<int>{i,j}); } } return res; } void dfs(vector<vector<int>>& matrix, int i, int j, bool& flag1, bool& flag2, vector<vector<bool>>& marked) { if ((flag1 && flag2) || marked[i][j])return; int dir[4][2] = {{1,0},{-1,0},{0,1},{0,-1}}; marked[i][j] = true; for (auto d: dir) { if (i+d[0] < 0 || j+d[1] < 0) // 当前格子已经第一排或第一列了 { flag1 = true; continue; } if (i+d[0] >= matrix.size() || j+d[1] >= matrix[0].size()) // 当前格子已经最后一排或者最后一列了 { flag2 = true; continue; } // 下一个方格比当前的低,并且之前没有走过 if(matrix[i+d[0]][j+d[1]] <= matrix[i][j] && !marked[i+d[0]][j+d[1]]) dfs(matrix, i+d[0], j+d[1], flag1, flag2, marked); if (flag1 && flag2)return; } } };
解法2:DFS从太平洋和大西洋往内部遍历
class Solution { public: vector<vector<int>> pacificAtlantic(vector<vector<int>>& matrix) { vector<vector<int>>res; int m = matrix.size(); if (m < 1)return res; int n = matrix[0].size(); if (n < 1)return res; vector<vector<bool>> marked1(m, vector<bool>(n, false)); vector<vector<bool>> marked2(m, vector<bool>(n, false)); for (int i = 0;i < m; ++i) { dfs(matrix, i, 0, marked1); dfs(matrix, i, n-1, marked2); } for (int i = 0;i < n;++i) { dfs(matrix, 0, i, marked1); dfs(matrix, m-1, i, marked2); } for (int i = 0; i < m; ++i) { for(int j = 0; j < n; ++j) { if (marked1[i][j] && marked2[i][j])res.push_back(vector<int>{i,j}); } } return res; } void dfs(vector<vector<int>>& matrix, int i, int j, vector<vector<bool>>& marked) { if (marked[i][j])return; int dir[4][2] = {{1,0},{-1,0},{0,1},{0,-1}}; marked[i][j] = true; for (auto d: dir) { int i_next = i + d[0]; int j_next = j + d[1]; if(i_next < 0 || i_next >= matrix.size() || j_next < 0 || j_next >= matrix[0].size() || matrix[i][j] > matrix[i_next][j_next]) continue; dfs(matrix, i+d[0], j+d[1], marked); } } };
以上是关于417. Pacific Atlantic Water Flow的主要内容,如果未能解决你的问题,请参考以下文章
417. Pacific Atlantic Water Flow
417 Pacific Atlantic Water Flow 太平洋大西洋水流
Pacific Atlantic Water Flow 解答
Leetcode: Pacific Atlantic Water Flow