第四周:( LeetCode417 ) Pacific Atlantic Water Flow(c++)

原题:
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:
The order of returned grid coordinates does not matter.
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).

思路:该题是经典的广度优先遍历的题。题目的大致意思是矩阵中每个点的值代表该处陆地的海拔,海拔高的点水可以流向其四周(上、下、左、右)低处(指小于等于该点海拔的地方)。Pacific在左边和上边的边缘,Atlantic在右边和下边的边缘,求矩阵中可以同时将水流向这两个海洋的高点的坐标的集合。本题笔者遍历所有的点(m*n),每个点分别通过bfs确认是否可以同时走到两个海洋的边缘。时间复杂度为O((M*N)^2) ,226ms通过。后面看了大神的思路,其实可以采用逆向思维,从矩阵的四条边做bfs,这样复杂度可以得到明显的降低,为O((M+N)*(MN))。由于时间的关系,没有写这种思路的代码。

代码:

#include <algorithm>
#include <queue>

class Solution {
public:
    bool bfs(int x,int y,vector<vector<int>>& matrix){
        int m,n,val;
        m=matrix.size();
        n=matrix[0].size();
        bool is_visit[m][n];
        bool Pacific=false,Atlantic=false;
        queue<pair<int, int>> q;
        memset(is_visit,false,sizeof(is_visit));
        is_visit[x][y]=true;
        while(q.size()>0)
            q.pop();
        q.push(make_pair(x,y));
        while(q.size()>0){
            x=q.front().first;
            y=q.front().second;
            val=matrix[x][y];
            if((x==0)||(y==0))
                Pacific=true;
            if((x==m-1)||(y==n-1))
                Atlantic=true;
            if(Pacific && Atlantic)
                break;
            if((x-1>=0)&&(!is_visit[x-1][y])&& (matrix[x-1][y]<=val)){
                q.push(make_pair(x-1,y));
                is_visit[x-1][y]=true;
            }
            if((x+1<m)&&(!is_visit[x+1][y])&& (matrix[x+1][y]<=val)){
                q.push(make_pair(x+1,y));
                is_visit[x+1][y]=true;
            }
            if((y-1>=0)&&(!is_visit[x][y-1])&& (matrix[x][y-1]<=val)){
                q.push(make_pair(x,y-1));
                is_visit[x][y-1]=true;
            }
            if((y+1<n)&&(!is_visit[x][y+1])&& (matrix[x][y+1]<=val)){
                q.push(make_pair(x,y+1));
                is_visit[x][y+1]=true;
            }
            q.pop();
        }
        if(Pacific && Atlantic)
            return true;
        else
            return false;
    }

    vector<pair<int, int>> pacificAtlantic(vector<vector<int>>& matrix) {
        vector<pair<int, int>> result;
        result.clear();
        if(matrix.size()==0)
            return result;
        int m,n;
        m=matrix.size();
        n=matrix[0].size();
        for(int i=0;i<m;i++){
            for(int j=0;j<n;j++){
                if(bfs(i,j,matrix))
                    result.push_back(make_pair(i,j));
            }
        }
        return result;
    }
};
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值