zoukankan      html  css  js  c++  java
  • leetcode 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:

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

    嗯,两边bfs,记录每一次bfs走过的点,对于一个点,如果两边bfs都经过,那么这个点就是所求的点

    边看电视,边写题....代码写的有点乱 竟然一次ac了....

    class Solution {
    public:
        int vis[200][200];
        int dir[4][2]={1,0,0,1,0,-1,-1,0};
        int v[200][200];
        vector<pair<int, int>> pacificAtlantic(vector<vector<int>>& matrix) {
            int n = matrix.size();
            vector<pair<int, int> >w;
            if (n==0) return w;
            int m = matrix[n-1].size();
            for (int i = 0; i < 200; ++i) for (int j = 0; j < 200; ++j) v[i][j]=vis[i][j] = 0;
            queue<pair<int,int> > q;
            //q.push({0,0});
            for (int i = 0; i < n;++i){
                v[i][0]++;
                vis[i][0]=1;
                q.push({i,0});
            }
            for (int j = 0; j < m; ++j){
                v[0][j]++;
                vis[0][j]=1;
                q.push({0,j});
            }
            while(!q.empty()) {
                pair<int,int> u = q.front();q.pop();
                int x = u.first;
                int y = u.second;
                for(int i = 0; i < 4; ++i) {
                    int nx = x + dir[i][0];
                    int ny = y + dir[i][1];
                    if (nx>=0&&ny>=0&&ny<m&&nx<n&&!vis[nx][ny]&&matrix[nx][ny]>=matrix[x][y]) {
                        q.push({nx,ny});
                        v[nx][ny]++;
                        vis[nx][ny]=1;
                    }
                }
            }
            while(!q.empty())q.pop();
            
            
            for (int i = 0; i < 200; ++i) for (int j = 0; j < 200; ++j) vis[i][j] = 0;
            for (int i = 0; i < n;++i){
                v[i][m-1]++;
                vis[i][m-1]=1;
                q.push({i,m-1});
            }
            for (int j = 0; j < m; ++j){
                v[n-1][j]++;
                vis[n-1][j]=1;
                q.push({n-1,j});
            }
            
            while(!q.empty()) {
                pair<int,int> u = q.front();q.pop();
                int x = u.first;
                int y = u.second;
                for(int i = 0; i < 4; ++i) {
                    int nx = x + dir[i][0];
                    int ny = y + dir[i][1];
                    if (nx>=0&&ny>=0&&ny<m&&nx<n&&!vis[nx][ny]&&matrix[nx][ny]>=matrix[x][y]) {
                        q.push({nx,ny});
                        v[nx][ny]++;
                        vis[nx][ny]=1;
                    }
                }
            }
            v[0][0]--;
            v[n-1][m-1]--;
            for (int i = 0; i < n; ++i) {
                for (int j = 0; j < m; ++j) {
                    cout << v[i][j] << " ";
                    if (v[i][j] >=2) w.push_back({i,j});
                }
                cout <<endl;
            }
            return w;
        }
    };
  • 相关阅读:
    spring声明式事务
    spring的传播行为和隔离级别
    索引(index)
    存储过程(转)
    Java中ArrayList相关的5道面试题
    记CVTE2014年春季招聘实习生求职历程
    Java中String,StringBuffer,StringBuilder的区别及其使用
    Linux下C程序的编译,运行,及调试
    skynet源码分析之skynet_server
    skynet源码分析之skynet_module
  • 原文地址:https://www.cnblogs.com/pk28/p/7413622.html
Copyright © 2011-2022 走看看