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;
        }
    };
  • 相关阅读:
    Windows下快速搭建安卓开发环境android-studio
    使用Android Studio搭建Android集成开发环境
    手动安装配置Android Studio
    android studio 各种问题 应该能帮助到你们
    如何清除XP的网络共享密码
    一个语言的“入流”,而是和这种语言进入某一子行业的契机有关
    必须冷静、必须听话,赶紧走
    QWidget继承自QPaintDevice,这样就可以直接把QWidget传入QPainter的构造函数,比如QPainter(mylabel),然后设置QWidget的长宽后直接进行作画了
    ActiveMQ
    开源word操作组件DocX的记录
  • 原文地址:https://www.cnblogs.com/pk28/p/7413622.html
Copyright © 2011-2022 走看看