首页 > 代码库 > 417. Pacific Atlantic Water Flow
417. Pacific Atlantic Water Flow
Problem statement:
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 * * * * * * AtlanticReturn:[[0, 4], [1, 3], [1, 4], [2, 2], [3, 0], [3, 1], [4, 0]] (positions with parentheses in above matrix).
Solution one: BFS
This is a 2D matrix DFS and BFS problem. According to what described, the start(or end) position are four lines. the top and left lines merge into Pacific, and the right and bottom lines merge in to Atlantic. The answer needs all positions that can merge into both Pacific and Atlantic.
Push the positions in top and left bottom boundaries into a queue, do BFS to find all positions that can merge into Pacific.
The same for Atlantic, and find the intersection.
Keep two visited matrix to indicate whether current point has already marked or not. Always pass the marked position(means could merge)(time pruning) and find candidates from unmarked.
Time complexity is O(m * n).
class Solution {public: vector<pair<int, int>> pacificAtlantic(vector<vector<int>>& matrix) { if(matrix.empty()){ return {}; } int row = matrix.size(); int col = matrix[0].size(); queue<pair<int, int>> pacific_que; queue<pair<int, int>> atlantic_que; vector<vector<int>> in_pacific(row, vector<int>(col, 0)); vector<vector<int>> in_atlantic(row, vector<int>(col, 0)); vector<pair<int, int>> coordinates; initialize(in_pacific, in_atlantic, pacific_que, atlantic_que, row, col); bfs(matrix, in_pacific, pacific_que, row, col); bfs(matrix, in_atlantic, atlantic_que, row, col); check(in_pacific, in_atlantic, coordinates, row, col); return coordinates; } private: void initialize(vector<vector<int>>& in_pacific, vector<vector<int>>& in_atlantic, queue<pair<int, int>>& pacific_que, queue<pair<int, int>>& atlantic_que, int row, int col){ // initialize visited matrix for(int j = 0; j < col; j++){ in_pacific[0][j] = 1; pacific_que.push({0, j}); in_atlantic[row - 1][j] = 1; atlantic_que.push({row - 1, j}); } for(int i = 0; i < row; i++){ in_pacific[i][0] = 1; pacific_que.push({i, 0}); in_atlantic[i][col - 1] = 1; atlantic_que.push({i, col - 1}); } } void bfs(vector<vector<int>>& matrix, vector<vector<int>>& visited, queue<pair<int, int>>& que, int row, int col){ vector<pair<int,int>> dirs = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; while(!que.empty()){ pair<int, int> cur_posi = que.front(); que.pop(); visited[cur_posi.first][cur_posi.second] = 1; for(auto dir : dirs){ int ix = cur_posi.first + dir.first; int iy = cur_posi.second + dir.second; if(ix >= 0 && ix < row && iy >= 0 && iy < col && visited[ix][iy] == 0 && matrix[ix][iy] >= matrix[cur_posi.first][cur_posi.second]){ que.push({ix, iy}); } } } return; } void check(vector<vector<int>>& pacific, vector<vector<int>> atlantic, vector<pair<int, int>>& coordinates, int row, int col){ for(int ix = 0; ix < row; ix++){ for(int iy = 0; iy < col; iy++){ if(pacific[ix][iy] & atlantic[ix][iy] == 1){ coordinates.push_back({ix, iy}); } } } return; }};
417. Pacific Atlantic Water Flow