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

太平洋大西洋水流问题

有一个 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会自己找路径,起始的这圈启动了就行。

相关推荐
卡卡~~18 小时前
“101计划”核心课程实践教材《数据实验结构指导-C++语言版》题目集
开发语言·c++·学习·算法
企业数字化笔记18 小时前
AI视频增强后画质怎么验收?插值基线、伪细节与帧间闪烁的检查方法
人工智能·算法·音视频
学逆向的19 小时前
C 内存属性
c语言·开发语言·算法·网络安全
lengxuenong19 小时前
树状数组 · 信息学学习手册1-求和3个模板
c++·学习·算法·青少年编程
ComputerInBook19 小时前
泛函分析概观
人工智能·算法·机器学习·泛函分析
Nuanyt20 小时前
LeetCode Hot100 刷题笔记 · 普通数组专题
java·笔记·算法·leetcode
q275513004220 小时前
AS8132 DP转HDMI 4K60(420)被动式 国产性价比AS方案
linux·算法·硬件架构·dash
安全指北针20 小时前
密态计算:数据 “可用不可见” 的底层技术解析
人工智能·算法
Jmyd012320 小时前
3D 扫描仪选型技术拆解:精度、点云与软件链路
人工智能·算法·3d·三维数字化