Pacific Atlantic Water Flow

题目来源
一个矩阵,水能从高往低流,左边上边是太平洋,右边下边是大西洋,问哪些地方既可以流往太平洋,又能够流往大西洋。
我用dfs的方法来做,但是写起来还是挺烦人的,感觉应该会有更好的更简洁的方法,代码如下:

class Solution {
public:
    vector> dirs = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
    vector> pacificAtlantic(vector>& matrix) {
        int n = matrix.size();
        vector> res;
        if (n == 0)
            return res;
        int m = matrix[0].size();
        vector>> closePA(n, vector>(m, make_pair(0, 0)));
        for (int i=0; i>& matrix, vector>>& closePA)
    {
        for (int i=0; i= 0 && newRow < matrix.size() && newCol >= 0 && newCol < matrix[0].size() && 
                matrix[newRow][newCol] >= matrix[row][col] && 
                (closePA[newRow][newCol].first < closePA[row][col].first || 
                closePA[newRow][newCol].second < closePA[row][col].second)) {
                closePA[newRow][newCol].first = max(closePA[newRow][newCol].first, closePA[row][col].first);
                closePA[newRow][newCol].second = max(closePA[newRow][newCol].second, closePA[row][col].second);
                dfs (newRow, newCol, matrix, closePA);
            }
        }
    }
};

你可能感兴趣的:(Pacific Atlantic Water Flow)