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;
        }
    };
  • 相关阅读:
    zendstudio 默认网页打开your project的时候不显示本地主机localhost的解决方法
    centos安装epel源后,使用报错(Error: Cannot retrieve repository metadata (repomd.xml) for repository: epel. Please verify its path and try again)
    描述硬链接和软链接区别
    软硬链接和文件之间的关系
    硬链接 inode号码相同 文件名不同
    目录下没有任何内容,为什么该目录的硬链接数为何是2
    磁盘还有空间为什么不能写入数据以及彻底删除文件原理
    inode和block的关系
    block
    /etc/init.d和/etc/rc.d/rc*.脚本/etc/rc.d/rc.local和/etc/rc.d/rc.sysinit
  • 原文地址:https://www.cnblogs.com/pk28/p/7413622.html
Copyright © 2011-2022 走看看