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;
    }
};

 

posted on 2017-08-22 20:55  Beserious  阅读(300)  评论(0编辑  收藏  举报