leetcode 417. Pacific Atlantic current

Keywords: less

A nonnegative integer matrix of m x n is given to represent the height of each cell in a continent. The Pacific Ocean is on the left and the upper boundary of the continent, while the Atlantic Ocean is on the right and the lower boundary of the continent.
It is specified that water flow can only flow in the up, down, left and right directions, and can only flow from high to low or at the same height.
Please find out the coordinates of land units that can flow to both the Pacific Ocean and the Atlantic Ocean.
Tips:
The order of output coordinates is not important
m and n are less than 150
Example:
Given the following 5x5 matrix:
Pacific Ocean ~ ~ ~ ~~
~ 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 Ocean
Return:
[[0, 4], [1, 3], [1, 4], [2, 2], [3, 0], [3, 1], [4, 0]] (bracketed unit in the above figure)

  • Method: dfs + memory search
  • I have two parts to deal with. The first dfs deals with whether it can reach the Pacific Ocean, and the second dfs deals with whether it can reach the Atlantic Ocean. I record the path I have traveled with path, if it is true
    If it is false, it cannot be reached. (search up, down, left and right for each point)
class Solution {
    public boolean[][] vis;
    public boolean[][] vis1;
    public int n,m;
    public List<int[]> pacificAtlantic(int[][] matrix) {
        if(matrix.length==0||matrix[0].length==0){
            return new ArrayList<int[]>();
        }
        n=matrix.length;
        m=matrix[0].length;
        boolean[][] path=new boolean[n][m];
        boolean[][] path1=new boolean[n][m];
        vis=new boolean[n][m];
        vis1=new boolean[n][m];
        List<int[]> ans=new ArrayList<int[]>();
        for(int i=0;i<n;++i){
            for(int j=0;j<m;++j){
                if(dfs(i,j,path,matrix,matrix[i][j])&&dfs1(i,j,path1,matrix,matrix[i][j])){
                    ans.add(new int[]{i,j});
                }
            }
        }
        return ans;
    }
    public boolean dfs(int x,int y,boolean[][] path,int[][] matrix,int pre){
        if(x>=n||y>=m){
            return false;
        }
        if(x<0||y<0){
            return true;
        }
        if(matrix[x][y]>pre){
            return false;
        }
        if(vis[x][y]){
            return path[x][y];
        }
        vis[x][y]=true;
        path[x][y]=dfs(x-1,y,path,matrix,matrix[x][y])||dfs(x,y-1,path,matrix,matrix[x][y])
        ||dfs(x+1,y,path,matrix,matrix[x][y])||dfs(x,y+1,path,matrix,matrix[x][y]);
        vis[x][y]=false;
        return path[x][y];
    }

    public boolean dfs1(int x,int y,boolean[][] path,int[][] matrix,int pre){
        if(x<0||y<0){
            return false;
        }
        if(x>=n||y>=m){
            return true;
        }
        if(matrix[x][y]>pre){
            return false;
        }
        if(vis1[x][y]){
            return path[x][y];
        }
        vis1[x][y]=true;
        path[x][y]=dfs1(x-1,y,path,matrix,matrix[x][y])||dfs1(x,y-1,path,matrix,matrix[x][y])
        ||dfs1(x+1,y,path,matrix,matrix[x][y])||dfs1(x,y+1,path,matrix,matrix[x][y]);
        vis1[x][y]=false;
        return path[x][y];
    }
}

Posted by Voodoo Jai on Thu, 02 Jan 2020 01:07:25 -0800