美文网首页
417. Pacific Atlantic Water Flow

417. Pacific Atlantic Water Flow

作者: 阿团相信梦想都能实现 | 来源:发表于2016-12-13 09:04 被阅读0次
    depth first search solution 
    class Solution(object):
        def pacificAtlantic(self, matrix):
            """
            :type matrix: List[List[int]]
            :rtype: List[List[int]]
            """
            if not matrix: return []
            self.directions = [(1,0),(-1,0),(0,1),(0,-1)]
            m = len(matrix)
            n = len(matrix[0])
            p_visited = [[False for _ in range(n)] for _ in range(m)]
            a_visited = [[False for _ in range(n)] for _ in range(m)]
            result = []
            
            for i in range(m):
                
                self.dfs(matrix, i, 0, p_visited, m, n)
                self.dfs(matrix, i, n-1, a_visited, m, n)
            for j in range(n):
               
                self.dfs(matrix, 0, j, p_visited, m, n)
                self.dfs(matrix, m-1, j, a_visited, m, n)
            
            for i in range(m):
                for j in range(n):
                    if p_visited[i][j] and a_visited[i][j]:
                        result.append([i,j])
            return result
                    
                    
        def dfs(self, matrix, i, j, visited, m, n):
            # when dfs called, meaning its caller already verified this point 
            #print 'visited',i,j
            visited[i][j] = True
            for dir in self.directions:
                x, y = i + dir[0], j + dir[1]
                #print 'trying to visit', x,y
                if x < 0 or x >= m or y < 0 or y >= n or visited[x][y] or matrix[x][y] < matrix[i][j]:
                   
                    continue
                self.dfs(matrix, x, y, visited, m, n)
    
    

    相关文章

      网友评论

          本文标题:417. Pacific Atlantic Water Flow

          本文链接:https://www.haomeiwen.com/subject/igmvmttx.html