【LeetCode】417. Pacific Atlantic Water Flow Add to List

题目:

Given an m x n matrix of non-negative integers representing the height of each unit cell in a continent, the "Pacific ocean" touches the left and top edges of the matrix and the "Atlantic ocean" touches the right and bottom edges.

Water can only flow in four directions (up, down, left, or right) from a cell to another one with height equal or lower.

Find the list of grid coordinates where water can flow to both the Pacific and Atlantic ocean.

Note:

  1. The order of returned grid coordinates does not matter.
  2. Both m and n are less than 150.

Example:

Given the following 5x5 matrix:

  Pacific ~   ~   ~   ~   ~ 
       ~  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  *
          *   *   *   *   * Atlantic

Return:

[[0, 4], [1, 3], [1, 4], [2, 2], [3, 0], [3, 1], [4, 0]] (positions with parentheses in above matrix).

分析:

这道题给了一个表示陆地高度的矩阵,高度高的水流可以流到高度低的地方,矩阵的上方和左方表示太平洋,下方和右方表示大西洋。要求出矩阵中的哪些位置既可以到达大西洋又可以到达太平洋。

我的想法是用两个向量来分别存储可以到达大西洋和到达太平洋的位置。通过宽度优先搜索对矩阵的四个边上的元素进行遍历,找到可以流入海洋的位置。然后找出两个向量中共同的元素。

代码:

class Solution {
public:
    vector<pair<int, int>> pacificAtlantic(vector<vector<int>>& matrix) {
        vector<pair<int,int>> toPacific;
        vector<pair<int,int>> toAtlantic;
        vector<pair<int,int>> result;
       
        int m = matrix.size();
        if(m == 0) return result;
        int n = matrix[0].size();
        int visited[150][150];
        for(int i = 0;i<m;i++)
        {
            for(int j = 0;j<n;j++)
                visited[i][j] = 0;
        }
        for(int i = 0;i<m;i++)
        {
         
         if(visited[i][0]!=1)
         {
       toPacific.push_back(pair<int,int>(i,0));
          visited[i][0] = 1;
                rec(toPacific,visited,matrix,i,0);
         }
        }
        for(int i = 1;i<n;i++)
        {
         if(visited[0][i] != 1)
         {
          toPacific.push_back(pair<int,int>(0,i));
          visited[0][i] = 1;
                rec(toPacific,visited,matrix,0,i);
         }
        } 
        for(int i = 0;i<m;i++)
        {
            for(int j = 0;j<n;j++)
                visited[i][j] = 0;
        }
        for(int i = 0;i<m;i++)
        {
         if(visited[i][n-1] == 0)
         {
          toAtlantic.push_back(pair<int,int>(i,n-1));
             visited[i][n-1] = 1;
                rec(toAtlantic,visited,matrix,i,n-1);
         }
        }
        for(int i = 0;i<n;i++)
        {
         if(visited[m-1][i] == 0)
         {       
       toAtlantic.push_back(pair<int,int>(m-1,i));
              visited[m-1][i] = 1;
           
               rec(toAtlantic,visited,matrix,m-1,i);
          
         }
        } 
        vector<pair<int,int>>::iterator search;
        for(int i = 0;i<toPacific.size();i++)
        {
            search=find(toAtlantic.begin(),toAtlantic.end(),toPacific[i]);
            if(search!=toAtlantic.end())
            result.push_back(toPacific[i]);
        }
        return result;
       
    }
    void rec(vector<pair<int,int>>& to,int visited[150][150],vector<vector<int>>& matrix,int a,int b)
    {
        int m = matrix.size();
        int n = matrix[0].size();
        queue<pair<int,int>> q;
        q.push(pair<int,int>(a,b));
        while(!q.empty())
        {
            int f = q.front().first;
            int s = q.front().second;
            q.pop();
            for(int i = -1;i<=1;i++)
            {
                for(int j = -1;j<=1;j++)
                {
                 if(i!=0&&j!=0) continue;
                    if(f+i>=0&&s+j>=0&f+i<m&&s+j<n)
                    {
                        if(matrix[f+i][s+j]>=matrix[f][s]&&visited[f+i][s+j]!=1)
                        {
                           q.push(pair<int,int>(f+i,s+j));
                           to.push_back(pair<int,int>(f+i,s+j));
                           visited[f+i][s+j] = 1;                         
                        }
                    }
                }
            }
        }
       
    }
   
};


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值