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

太平洋大西洋水流问题

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

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

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

返回网格坐标 result 的 2D 列表 ,其中 resulti = 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 == heightsr.length

1 <= m, n <= 200

0 <= heightsrc <= 105

阅读理解:

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

思路

想到是bfs遍历每个点尝试走到左上和右下,最后左上和右下标志位同为真就是高点。

写了下,没写出来,记一下这个题

代码

dfs:

cpp 复制代码
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:

cpp 复制代码
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会自己找路径,起始的这圈启动了就行。

相关推荐
watersink8 分钟前
机器学习聚类算法
算法·机器学习·聚类
luj_17681 小时前
桥牌思维启示:系统设计的模块化架构
c语言·开发语言·c++·经验分享·算法
饼饼学习空间智能3 小时前
家庭服务机器人训练数据怎么积累?仿真、真实采集与持续学习的技术路线分析
人工智能·算法·机器学习
en.en..4 小时前
数据结构考点解析:逻辑闭环与存储循环辨析
数据结构
蛋先生DX4 小时前
你瘦不下来但大模型可以:量化原理了解一下
深度学习·算法·llm
Scabbards_4 小时前
面试Leetcode - Heap 堆
java·leetcode·面试
牛阿大5 小时前
LQR算法
算法
(╹◡╹)6 小时前
18.剪枝
算法·机器学习·剪枝
布莱克6056 小时前
数据库索引分类:数据结构、物理存储与逻辑角度详解
数据结构·数据库