• Leetcode 1020. Number of Enclaves


    dfs或者bfs

    class Solution:
        def dfs(self, A, rows, cols, i, j):
            if not (0 <= i < rows and 0 <= j < cols):
                return
            elif A[i][j] == 0:
                return
            else:
                A[i][j] = 0
                self.dfs(A, rows, cols, i + 1, j)
                self.dfs(A, rows, cols, i - 1, j)
                self.dfs(A, rows, cols, i, j + 1)
                self.dfs(A, rows, cols, i, j - 1)
    
        def numEnclaves(self, A: List[List[int]]) -> int:
            rows = len(A)
            cols = len(A[0])
            for j in range(0, cols):
                if A[0][j] == 1:
                    self.dfs(A, rows, cols, 0, j)
                if A[rows - 1][j] == 1:
                    self.dfs(A, rows, cols, rows - 1, j)
    
            for i in range(0, rows):
                if A[i][0] == 1:
                    self.dfs(A, rows, cols, i, 0)
                if A[i][cols - 1] == 1:
                    self.dfs(A, rows, cols, i, cols - 1)
    
            ans = 0
            for row in A:
                for a in row:
                    if a == 1:
                        ans += 1
            return ans

    或者

    import queue
    class Solution:
        def bfs(self, A, rows, cols, i, j):
            q = queue.Queue()
            A[i][j] = 0
            q.put((i, j))
            while not q.empty():
                p = q.get()
                if 0 <= p[0] + 1 < rows and A[p[0] + 1][p[1]] == 1:
                    A[p[0] + 1][p[1]] = 0
                    q.put((p[0] + 1, p[1]))
                if 0 <= p[0] - 1 < rows and A[p[0] - 1][p[1]] == 1:
                    A[p[0] - 1][p[1]] = 0
                    q.put((p[0] - 1, p[1]))
                if 0 <= p[1] + 1 < cols and A[p[0]][p[1] + 1] == 1:
                    A[p[0]][p[1] + 1] = 0
                    q.put((p[0], p[1] + 1))
                if 0 <= p[1] - 1 < cols and A[p[0]][p[1] - 1] == 1:
                    A[p[0]][p[1] - 1] = 0
                    q.put((p[0], p[1] - 1))
    
        def numEnclaves(self, A: List[List[int]]) -> int:
            rows = len(A)
            cols = len(A[0])
            for j in range(0, cols):
                if A[0][j] == 1:
                    self.bfs(A, rows, cols, 0, j)
                if A[rows - 1][j] == 1:
                    self.bfs(A, rows, cols, rows - 1, j)
    
            for i in range(0, rows):
                if A[i][0] == 1:
                    self.bfs(A, rows, cols, i, 0)
                if A[i][cols - 1] == 1:
                    self.bfs(A, rows, cols, i, cols - 1)
    
            ans = 0
            for row in A:
                for a in row:
                    if a == 1:
                        ans += 1
            return ans
  • 相关阅读:
    Linux环境变量文件
    Hadoop的ERROR: Attempting to operate on yarn resourcemanager as root的解决方法
    如何启用WSS3.0的匿名访问?
    Microsoft Windows SharePoint Services 3.0
    在sps页面中在新窗口打开文档
    wss3.0安装时的bug
    使用GROUP BY子句的规则
    8种Nosql数据库系统对比
    django学习
    Python socket编程
  • 原文地址:https://www.cnblogs.com/zywscq/p/10739680.html
Copyright © 2020-2023  润新知