vector<pair<int, int>> pacificAtlantic(vector<vector<int>>& matrix) { vector<pair<int,int>> res; if(!matrix.size() || !matrix[0].size()) return res; int m = matrix.size(), n = matrix[0].size(); queue<pair<int,int>> qA, qP; vector<vector<bool>> pacific(m,vector<bool>(n, false)); vector<vector<bool>> atlantic(m,vector<bool>(n, false)); vector<pair<int,int>> dir{{1,0},{-1,0},{0,1},{0,-1}}; for(int i = 0; i < m; ++i){ qP.push({i,0}); qA.push({i,n-1}); pacific[i][0] = true; atlantic[i][n-1] = true; } for(int i = 0; i < n; ++i){ qP.push({0,i}); qA.push({m-1,i}); pacific[0][i] = true; atlantic[m-1][i] = true; } while(!qP.empty()){ pair<int,int> cur = qP.front(); qP.pop(); for(auto d : dir){ int x = cur.first+d.first, y = cur.second+d.second; if(x>=0&&y>=0&&x<m&&y<n&&!pacific[x][y]&&matrix[x][y] >= matrix[cur.first][cur.second]){ pacific[x][y] = true; qP.push({x,y}); } } } while(!qA.empty()){ pair<int,int> cur = qA.front(); qA.pop(); for(auto d : dir){ int x = cur.first+d.first, y = cur.second+d.second; if(x>=0&&y>=0&&x<m&&y<n&&!atlantic[x][y]&&matrix[x][y] >= matrix[cur.first][cur.second]){ atlantic[x][y] = true; qA.push({x,y}); } } } for(int i = 0; i < m; ++i){ for(int j = 0; j < n; ++j){ if(pacific[i][j] && atlantic[i][j]) res.push_back({i,j}); } } return res; }
vector<pair<int, int>> pacificAtlantic(vector<vector<int>>& matrix) { if (matrix.empty() || matrix[0].empty()) return {}; vector<pair<int, int>> res; int m = matrix.size(), n = matrix[0].size(); vector<vector<bool>> pacific(m, vector<bool>(n, false)); vector<vector<bool>> atlantic(m, vector<bool>(n, false)); for (int i = 0; i < m; ++i) { dfs(matrix, pacific, INT_MIN, i, 0); dfs(matrix, atlantic, INT_MIN, i, n - 1); } for (int i = 0; i < n; ++i) { dfs(matrix, pacific, INT_MIN, 0, i); dfs(matrix, atlantic, INT_MIN, m - 1, i); } for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { if (pacific[i][j] && atlantic[i][j]) { res.push_back({i, j}); } } } return res; }