【做一道算一道】太平洋大西洋水流问题

太平洋大西洋水流问题

有一个 m × n 的矩形岛屿,与 太平洋 和 大西洋 相邻。 “太平洋” 处于大陆的左边界和上边界,而 “大西洋” 处于大陆的右边界和下边界。

这个岛被分割成一个由若干方形单元格组成的网格。给定一个 m x n 的整数矩阵 heights , heights[r][c] 表示坐标 (r, c) 上单元格 高于海平面的高度 。

岛上雨水较多,如果相邻单元格的高度 小于或等于 当前单元格的高度,雨水可以直接向北、南、东、西流向相邻单元格。水可以从海洋附近的任何单元格流入海洋。

返回网格坐标 result 的 2D 列表 ,其中 result[i] = [ri, ci] 表示雨水从单元格 (ri, ci) 流动 既可流向太平洋也可流向大西洋 。
在这里插入图片描述

示例 1:

输入: heights = [[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]]
输出: [[0,4],[1,3],[1,4],[2,2],[3,0],[3,1],[4,0]]

示例 2:

输入: heights = [[2,1],[1,2]]
输出: [[0,0],[0,1],[1,0],[1,1]]

提示:

m == heights.length
n == heights[r].length
1 <= m, n <= 200
0 <= heights[r][c] <= 105

阅读理解:

找到给出的表格中的高点,高点定义为能从高点出发到达左或上边界以及到达右或下边界。

思路

想到是bfs遍历每个点尝试走到左上和右下,最后左上和右下标志位同为真就是高点。
写了下,没写出来,记一下这个题

代码

dfs:

class Solution {
public:
    int n, m;
    int dir[4][2] = {-1, 0, 0, -1, 1, 0, 0, 1};
    void dfs(vector<vector<int>>& grid, vector<vector<bool>>& visited, int x, int y) {
        if (visited[x][y]) return;

        visited[x][y] = true;

        for (int i = 0; i < 4; i++) {
            int nextx = x + dir[i][0];
            int nexty = y + dir[i][1];
            if (nextx < 0 || nextx >= n || nexty < 0 || nexty >= m) continue;
            if (grid[x][y] > grid[nextx][nexty]) continue; // 注意:这里是从低向高遍历

            dfs (grid, visited, nextx, nexty);
        }
        return;
    }
    vector<vector<int>> pacificAtlantic(vector<vector<int>>& heights) {
        n=heights.size();
        m=heights[0].size();
        vector<vector<bool>> pacific(n,vector<bool>(m,false));
        vector<vector<bool>> atlantic(n,vector<bool>(m,false));

        vector<vector<int>> res;
        //上下逼近
        for(int i=0;i<n;i++){
            dfs(heights,pacific,i,0);
            dfs(heights,atlantic,i,m-1);
        }
        //左右逼近
        for(int i=0;i<m;i++){
            dfs(heights,pacific,0,i);
            dfs(heights,atlantic,n-1,i);
        }
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                if(pacific[i][j]&&atlantic[i][j]){
                    res.push_back({i,j});
                }
            }
        }
        return res;
    }
};

bfs:

class Solution {
public:
    int n, m;
    int dir[4][2] = {-1, 0, 0, -1, 1, 0, 0, 1};
    void bfs(vector<vector<int>>& heights,vector<vector<bool>>& visited,int x,int y){
        if (visited[x][y]) return;
        
        queue<pair<int, int>> que;
        que.push({x, y});

        visited[x][y] = true; // 只要加入队列,立刻标记

        while(!que.empty()) {
        pair<int ,int> cur = que.front(); que.pop();
        int curx = cur.first;
        int cury = cur.second;
            for(int i=0;i<4;i++){
                int nextx=curx+dir[i][0];
                int nexty=cury+dir[i][1];
                if(nextx<0||nextx>=n||nexty<0||nexty>=m)
                continue;
                if (heights[x][y] > heights[nextx][nexty]) 
                continue; // 注意:这里是从低向高遍历
                bfs(heights,visited,nextx,nexty);
            }
            return;
        }
    }
    
    vector<vector<int>> pacificAtlantic(vector<vector<int>>& heights) {
        n=heights.size();
        m=heights[0].size();
        vector<vector<bool>> pacific(n,vector<bool>(m,false));
        vector<vector<bool>> atlantic(n,vector<bool>(m,false));

        vector<vector<int>> res;
        //上下逼近
        for(int i=0;i<n;i++){
            bfs(heights,pacific,i,0);
            bfs(heights,atlantic,i,m-1);
        }
        //左右逼近
        for(int i=0;i<m;i++){
            bfs(heights,pacific,0,i);
            bfs(heights,atlantic,n-1,i);
        }
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                if(pacific[i][j]&&atlantic[i][j]){
                    res.push_back({i,j});
                }
            }
        }
        return res;
    }
};

意识到bfs和dfs的遍历过程中原地改grid是不应该的,应该把dfs和bfs作工具,外部调用来操作找答案,应该添加的标记在visted中。

逼近过程:
bfs会自己找路径,起始的这圈启动了就行。
在这里插入图片描述

  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值