417.太平洋大西洋水流问题

417. 太平洋大西洋水流问题

  1. 题目链接:https://leetcode-cn.com/problems/pacific-atlantic-water-flow/

  2. 思路:

    1. 用深搜先找到能流入太平洋的位置,用一个数组储存
    2. 用深搜先找到能流入大西洋的位置,用一个数组储存
    3. 比较两个数组,找到既能流入太平洋又能流入大西洋的位置
  3. 代码:

    static const int dirs[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
    
    class Solution {
    public:
        vector<vector<int>> heights;
    
        void dfs(int row, int col, vector<vector<bool>> & ocean) {
            int m = ocean.size();
            int n = ocean[0].size();
            if (ocean[row][col]) {
                return;
            }
            ocean[row][col] = true;
            for (int i = 0; i < 4; i++) {
                int newRow = row + dirs[i][0], newCol = col + dirs[i][1];
                if (newRow >= 0 && newRow < m && newCol >= 0 && newCol < n && heights[newRow][newCol] >= heights[row][col]) {
                    dfs(newRow, newCol, ocean);
                }
            }
        }
    
        vector<vector<int>> pacificAtlantic(vector<vector<int>>& heights) {
            this->heights = heights;
            int m = heights.size();
            int n = heights[0].size();
            vector<vector<bool>> pacific(m, vector<bool>(n, false));
            vector<vector<bool>> atlantic(m, vector<bool>(n, false));
    
            for (int i = 0; i < m; i++) {
                dfs(i, 0, pacific);
            }
            for (int j = 1; j < n; j++) {
                dfs(0, j, pacific);
            }
            for (int i = 0; i < m; i++) {
                dfs(i, n - 1, atlantic);
            }
            for (int j = 0; j < n - 1; j++) {
                dfs(m - 1, j, atlantic);
            }
            vector<vector<int>> result;
            for (int i = 0; i < m; i++) {
                for (int j = 0; j < n; j++) {
                    if (pacific[i][j] && atlantic[i][j]) {
                        vector<int> cell;
                        cell.emplace_back(i);
                        cell.emplace_back(j);
                        result.emplace_back(cell);
                    }
                }
            }
            return result;
        }
    };
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以通过以下方法根据经纬度判断其所在的大洋: 1. 根据经度判断所在的半球,东经为正,西经为负。经度为正数时,位于东半球;经度为负数时,位于西半球。 2. 根据纬度判断所在的海洋区域。大洋分为南北两半球,赤道为分界线。在南北两半球中,纬度越小,距离赤道越近,纬度越大,距离极点越近。 3. 在南北两半球的基础上,根据经度和纬度的范围,判断其所在的海洋区域。太平洋大西洋、印度洋、南极洲洲际地区、北极洲洲际地区是五个大洋,其中太平洋大西洋、印度洋是最主要的三个洋。 以下是一个示例代码,可以根据经纬度判断其所在的大洋: ```python def get_ocean(lon, lat): if lon > 0: hemisphere = "东半球" else: hemisphere = "西半球" if lat < 0: latitude = "南半球" else: latitude = "北半球" if lat < -60: ocean = "南极洲洲际地区" elif lat >= -60 and lat < -10: ocean = "南大洋" elif lat >= -10 and lat < 0: ocean = "南极洋界" elif lat >= 0 and lat < 10: ocean = "赤道附近海域" elif lat >= 10 and lat < 20: ocean = "北赤道海域" elif lat >= 20 and lat < 30: ocean = "副赤道海域" elif lat >= 30 and lat < 60: ocean = "北大洋" elif lat >= 60: ocean = "北极洲洲际地区" if lon >= -180 and lon < -20: location = "太平洋" elif lon >= -20 and lon < 20: location = "印度洋" elif lon >= 20 and lon < 160: location = "太平洋" elif lon >= 160 and lon <= 180: location = "北大西洋" elif lon >= -180 and lon < -40: location = "南大西洋" elif lon >= -40 and lon < 20: location = "南美洲海岸" else: location = "未知" return hemisphere + " " + latitude + " " + location + " " + ocean ``` 该函数接受两个参数:经度 lon 和纬度 lat。根据经度和纬度的范围,判断其所在的海洋区域,最后返回一个字符串,该字符串包含了其所在的半球、海洋区域、大洋等信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值