417. 太平洋大西洋水流问题/C++

在这里插入图片描述
参考:https://blog.csdn.net/qq_41855420/article/details/88744535
分别从太平洋的每个点dfs,并记录
分别从大西洋的每个点dfs,并记录
然后遍历所有点,并查看以上2个记录即可。

class Solution {
    vector<vector<int>> res;
    int row,col;
    int d[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
    map<pair<int, int>, bool> pacificFlag;
    map<pair<int, int>, bool> atlanticFlag;
    
    bool inArea(int x,int y){
        return x>=0 && x<row && y>=0 && y<col;
    }
    
    void dfs(vector<vector<int>>& matrix,map<pair<int, int>, bool>& flag, int x,int y){
        flag[{x,y}]=true;
        for(int i=0;i<4;++i){
            int newX=x+d[i][0];
            int newY=y+d[i][1];
            if(inArea(newX,newY) && matrix[x][y]<=matrix[newX][newY] 
            	&& flag.count({newX,newY})==0){
                flag[{newX,newY}]=true;
                dfs(matrix,flag,newX,newY);
            }
        }
        return;
    }
    
   
public:
    vector<vector<int>> pacificAtlantic(vector<vector<int>>& matrix) {
        res.clear();
        row = matrix.size();
        if(row==0)
            return res;
        
        col=matrix[0].size();
        if(col==0)
            return res;
        
        for(int i=0;i<row;++i){
            dfs(matrix,pacificFlag,i,0);
            dfs(matrix,atlanticFlag,i,col-1);
        }
        
        for(int i=0;i<col;++i){
            dfs(matrix,pacificFlag,0,i);
            dfs(matrix,atlanticFlag,row-1,i);
        }
        
        for(int i=0;i<row;++i)
            for(int j=0;j<col;++j)
                if(pacificFlag[{i,j}] && atlanticFlag[{i,j}])
                    res.push_back({i,j});
        return res;
    }
};
  • 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、付费专栏及课程。

余额充值