417. Pacific Atlantic Water Flow(BFS)

1. 题目描述

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

2. 代码(最后一个case超时了)

leetcode上可AC的solution(BFS):参考

class Solution {
public:
    vector<pair<int, int> > pacificAtlantic(vector<vector<int> >& matrix) {

        int rows = matrix.size();
        int cols = 0;
        vector<pair<int, int> > res;

        if(rows > 0) cols = matrix[0].size();
        else return res;
        // initialized 0; 1:can reach both
        vector<vector<int> > canReached(rows,vector<int>(cols,0));

        for(int i = 0; i < rows; i++) {
            for(int j = 0; j < cols; j++) {
                bfs(matrix, canReached, i, j);
                if(canReached[i][j] == 1) {
                    res.push_back(make_pair(i,j));
                }
            }
        }
        return res;
    }

    void bfs(vector<vector<int> >& matrix, vector<vector<int> >& canReached, int i, int j) {    
        int rows = matrix.size();
        int cols = matrix[0].size();
        vector<vector<bool> > visited(rows,vector<bool>(cols,false));
        queue<pair<int, int> > bfs;
        bfs.push(make_pair(i,j));
        visited[i][j] = true;
        pair<int, int> tmp;
        bool ok1 = false, ok2 = false;
        if((i == 0 || j == 0)) {
            ok1 = true;
        }
        if(i == rows-1||j == cols-1) {
            ok2 = true;
        }
        if(ok1 && ok2) {
            canReached[i][j] = 1;
            return;
        }
        while(!bfs.empty()) {
            int size = bfs.size();
            for(int h = 0; h < size; h++) {
                tmp = bfs.front();
                bfs.pop();

                vector<pair<int, int> > next = canMove(matrix, visited, tmp.first, tmp.second);
                for(int k = 0; k < next.size(); k++) {
                    pair<int,int> node = next[k];
                    if(node.first == 0 ||node.second == 0) {
                        ok1 = true;
                    }
                    if(node.first == rows-1 || node.second == cols-1) {
                        ok2 = true;
                    }
                    if(ok1 && ok2) {
                        canReached[i][j] = 1;
                        return;
                    } else {
                        visited[node.first][node.second] = true;
                        bfs.push(node); 
                    }

                }
            }
        }
        return; 
    }

    vector<pair<int, int> > canMove(vector<vector<int> >& matrix, vector<vector<bool> >& visited, int i, int j) {
        vector<pair<int, int> > res;
        int rows = matrix.size();
        int cols = matrix[0].size();
        if(i-1 >= 0 && !visited[i-1][j] && matrix[i-1][j] <= matrix[i][j]) {
            res.push_back(make_pair(i-1,j));
        } 
        if(i+1 < rows && !visited[i+1][j] && matrix[i+1][j] <= matrix[i][j]) {
            res.push_back(make_pair(i+1, j));
        }
        if(j-1 >= 0 && !visited[i][j-1] && matrix[i][j-1] <= matrix[i][j]) {
            res.push_back(make_pair(i,j-1));
        }
        if(j+1 < cols && !visited[i][j+1] && matrix[i][j+1] <= matrix[i][j]) {
            res.push_back(make_pair(i, j+1));
        }
        return res;
    }
};

能解决超时的朋友,麻烦联系我,谢谢~
联系邮箱:sysuygm@163.com

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值