[LeetCode] 417. Pacific Atlantic Water Flow

417. Pacific Atlantic Water Flow

题目描述

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).

思路分析

思路比较简单,有两个vis数组,分别记录能到达pancific和altantic的位置,初始化为分别靠近这两个海洋的位置,然后从这两个位置开始进行bfs。最后通过遍历来获取同时能达到这两个海洋的位置。

时间复杂度分析

使用进行2m+ 2n次 bfs,但是实际上只是2次量级的也就是常数级。初始化需要一次遍历因此总的时间复杂度是 O ( m n L o g E )

代码

#include <iostream>
#include <vector>
#include <queue>

using namespace std;

class Solution {
public:
    struct point {
        int x, y;
        point(int _x = -1, int _y = -1) : x(_x), y(_y) {}
    };

    vector<pair<int, int>> pacificAtlantic(vector<vector<int>>& matrix) {
        m = matrix.size();
        if (m == 0) return ans;
        n = matrix[0].size();
        // first is pacific, second is atlantic
        flow.resize(m, vector<int>(n, 0));
        queue<point> p;
        queue<point> a;
        vector<vector<bool>> vis_p(m, vector<bool>(n, 0));
        vector<vector<bool>> vis_a(m, vector<bool>(n, 0));
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (i == 0 || j == 0) {
                    vis_p[i][j] = 1;
                    p.push(point(j, i));
                }
                if (i == m-1 || j == n-1) {
                    vis_a[i][j] = 1;
                    a.push(point(j,i));
                }
            }
        }
        bfs(matrix, vis_p, p);
        bfs(matrix, vis_a, a);
        for (int i =0; i<m; i++) {
            for (int j =0; j < n; j++) {
                if (vis_a[i][j] && vis_p[i][j]) ans.push_back(make_pair(i,j));
            }
        }
        return ans;
    }

    void bfs(vector<vector<int>>& matrix,vector<vector<bool>> &vis, queue<point> q) {
        while (!q.empty()) {
            point p = q.front();
            q.pop();
            for (int i = 0; i < 4; i++) {
                if (p.x + _x[i] >= 0 && p.x + _x[i] < n && p.y + _y[i] >=0 && p.y + _y[i] < m &&
                matrix[p.y][p.x] <= matrix[p.y + _y[i]][p.x + _x[i]] && !vis[p.y + _y[i]][p.x + _x[i]]) {
                    vis[p.y + _y[i]][p.x + _x[i]] = true;
                    q.push(point(p.x + _x[i], p.y + _y[i]));
                }
            }
        }
    }
    int _x[4] = {0, 0, 1, -1};
    int _y[4] = {1, -1, 0, 0};
    int m, n;
    vector<pair<int, int> > ans;
    vector<vector<int> > flow;
};

猜你喜欢

转载自blog.csdn.net/qq_34035179/article/details/82562204