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

来源:互联网 发布:淘宝淘宝联盟怎么用法 编辑:程序博客网 时间:2024/06/04 18:15

原题:
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  *          *   *   *   *   * AtlanticReturn:[[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;    }};
0 0
原创粉丝点击