417 Pacific Atlantic Water Flow

博客围绕矩阵水流流向问题展开,给定矩阵表示陆地地势,左右分别为太平洋和大西洋,水流可在平地或从高向低流动。初次尝试用动态规划失败,后采用广度优先遍历方法,从边界开始,用队列记录水流发源地,寻找地势更高处,直至队列为空。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1 题目

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:

  1. The order of returned grid coordinates does not matter.
  2. 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).

2 尝试解

2.1 分析

给定一块用矩阵表示的陆地,矩阵值表示地势高低。陆地左方和上方是太平洋,右方和下方是大西洋。水流可以在平地之间或从高向低流动,问哪些地方的水可以同时流向大西洋和太平洋。

初次尝试时,尝试用动态规划的方法,分别用两个矩阵分别表示可以流向大西洋和太平洋的地方,太平洋向右向下规划,大西洋向左向上规划。但是在矩阵[[123],[8,9,4],[7,6,5]]上失败了,原因是水流可以向右向下逆流向5,再逆流向6.,并不单纯只向右向下逆流。所以采用广度优先遍历的方法,从边界开始,用队列记录当前水流可以发源的地方,寻找所有还没有判断过的,地势比当前可以发源地更高的地方,加入队列,然后删除已经流过的地方,直到队列为空。

2.2 代码

class Solution {
public:
    int m = 0;
    int n = 0;
    vector<vector<int>> pacificAtlantic(vector<vector<int>>& matrix) {
        if(!matrix.size()) return {};
        m = matrix.size();
        n = matrix[0].size();
        vector<vector<int>> pacific(m,vector<int>(n,0)), atlantic(m,vector<int>(n,0)),result;
        backflow(pacific,matrix,make_pair(0,0));
        backflow(atlantic,matrix,make_pair(m-1,n-1));
        for(int i = 0; i < m; i++){
            for(int j = 0; j < n; j++){
                if(pacific[i][j]*atlantic[i][j]){
                    result.push_back({i,j});

                }
            }
        }
        return result;
    }
    void backflow(vector<vector<int>>&continent,vector<vector<int>>&matrix,pair<int,int> boundary){
        queue<pair<int,int>> cur;
        for(int i = 0; i < n; i++) {
            cur.emplace(make_pair(boundary.first,i));
            continent[boundary.first][i] = 1;
        }
        for(int i = 0; i < m; i++) {
            cur.emplace(make_pair(i,boundary.second));
            continent[i][boundary.second] = 1;
        }
        while(!cur.empty()){
            pair<int,int> temp = cur.front();
            cur.pop();
            if(temp.first > 0 && !continent[temp.first-1][temp.second] && matrix[temp.first-1][temp.second] >= matrix[temp.first][temp.second]){
                continent[temp.first-1][temp.second] = 1;
                cur.emplace(make_pair(temp.first-1,temp.second));
            }
            if(temp.first < m-1 && !continent[temp.first+1][temp.second] && matrix[temp.first+1][temp.second] >= matrix[temp.first][temp.second]){
                continent[temp.first+1][temp.second] = 1;
                cur.emplace(make_pair(temp.first+1,temp.second));
            }
            if(temp.second > 0 && !continent[temp.first][temp.second-1] && matrix[temp.first][temp.second-1] >= matrix[temp.first][temp.second]){
                continent[temp.first][temp.second-1] = 1;
                cur.emplace(make_pair(temp.first,temp.second-1));
            }
            if(temp.second < n-1 && !continent[temp.first][temp.second+1] && matrix[temp.first][temp.second+1] >= matrix[temp.first][temp.second]){
                continent[temp.first][temp.second+1] = 1;
                cur.emplace(make_pair(temp.first,temp.second+1));
            }            
        }
    }
};

3 标准解

class Solution {
public:
    vector<pair<int, int>> res;
    vector<vector<int>> visited;
    void dfs(vector<vector<int>>& matrix, int x, int y, int pre, int preval){
        if (x < 0 || x >= matrix.size() || y < 0 || y >= matrix[0].size()  
                || matrix[x][y] < pre || (visited[x][y] & preval) == preval) 
            return;
        visited[x][y] |= preval;
        if (visited[x][y] == 3) res.push_back({x, y});
        dfs(matrix, x + 1, y, matrix[x][y], visited[x][y]); dfs(matrix, x - 1, y, matrix[x][y], visited[x][y]);
        dfs(matrix, x, y + 1, matrix[x][y], visited[x][y]); dfs(matrix, x, y - 1, matrix[x][y], visited[x][y]);
    }

    vector<pair<int, int>> pacificAtlantic(vector<vector<int>>& matrix) {
        if (matrix.empty()) return res;
        int m = matrix.size(), n = matrix[0].size();
        visited.resize(m, vector<int>(n, 0));
        for (int i = 0; i < m; i++) {
            dfs(matrix, i, 0, INT_MIN, 1);
            dfs(matrix, i, n - 1, INT_MIN, 2);
        }
        for (int i = 0; i < n; i++) {
            dfs(matrix, 0, i, INT_MIN, 1);
            dfs(matrix, m - 1, i, INT_MIN, 2);
        }
        return res;
    }
};

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值