DFS 之岛屿问题 21.11.30

最大的岛屿

695. 岛屿的最大面积

模拟人丈量岛屿

从左上忘右下遍历,确定登陆岛屿后,按左右上下丈量岛屿面积,丈量过的记为2,同时记录最大岛屿面积

class Solution(object):
    def maxAreaOfIsland(self, grid):
        """
        :type grid: List[List[int]]
        :rtype: int
        """
        def compute(w, l, area):
            # 计算岛屿面积
            # 往上
            if w - 1 >= 0 and grid[w - 1][l] == 1:   # 条件要注意,等于0可以取到的
                grid[w - 1][l] = 2
                area = compute(w - 1, l, area + 1)
            # 往下
            if w + 1 < width and grid[w + 1][l] == 1:
                grid[w + 1][l] = 2
                area = compute(w + 1, l, area + 1)
            # 往左
            if l - 1 >= 0 and grid[w][l - 1] == 1:   # 等于零可取
                grid[w][l - 1] = 2
                area = compute(w, l - 1, area + 1)
            # 往右
            if l + 1 < length and grid[w][l + 1] == 1:
                grid[w][l + 1] = 2
                area = compute(w, l + 1, area + 1)
            return area
        length = len(grid[0])
        width = len(grid)
        theMax = 0
        for w in range(width):
            for l in range(length):
                if grid[w][l] == 1:
                    grid[w][l] = 2
                    # 如果登陆岛屿,进入丈量模式
                    theMax = max(theMax, compute(w, l, 1))
        return theMax
   
A = Solution()
grid = [[0,0,1,0,0,0,0,1,0,0,0,0,0],
        [0,0,0,0,0,0,0,1,1,1,0,0,0],
        [0,1,1,0,1,0,0,0,0,0,0,0,0],
        [0,1,0,0,1,1,0,0,1,0,1,0,0],
        [0,1,0,0,1,1,0,0,1,1,1,0,0],
        [0,0,0,0,0,0,0,0,0,0,1,0,0],
        [0,0,0,0,0,0,0,1,1,1,0,0,0],
        [0,0,0,0,0,0,0,1,1,0,0,0,0]]
grid = [[0,1],
        [1,1]]
print(A.maxAreaOfIsland(grid))

执行用时:40 ms, 在所有 Python 提交中击败了97.08%的用户

内存消耗:15.4 MB, 在所有 Python 提交中击败了37.70%的用户

self.maxArea = 0 
# 可以在类中的函数公用
# 普通的函数相比,在类中定义的函数只有两点点不同:
# 1、第一个参数永远是 self
# 2、在类中函数相互调用要加 self

200.岛屿数量

class Solution(object):
    def numIslands(self, grid):
        """
        :type grid: List[List[str]]
        :rtype: int
        """
        def compute(r, c):
            if r - 1 >= 0 and grid[r - 1][c] == "1":
                grid[r - 1][c] = "2"
                compute(r - 1, c)
            if r + 1 < row and grid[r + 1][c] == "1":
                grid[r + 1][c] = "2"
                compute(r + 1, c)
            if c - 1 >= 0 and grid[r][c - 1] == "1":
                grid[r][c - 1] = "2"
                compute(r, c - 1)
            if c + 1 < col and grid[r][c + 1] == "1":
                grid[r][c + 1] = "2"
                compute(r, c + 1)
        row = len(grid)
        col = len(grid[0])
        num = 0
        for r in range(row):
            for c in range(col):
                if grid[r][c] == "1":
                    grid[r][c] = "2"
                    compute(r, c)
                    num = num + 1
        return num

1905.统计子岛屿

class Solution(object):
    def countSubIslands(self, grid1, grid2):
        """
        :type grid1: List[List[int]]
        :type grid2: List[List[int]]
        :rtype: int
        """
        row = len(grid2)
        col = len(grid2[0])
        num = 0
        # 深搜grid2,同时看grid1能不能包含grid2的岛
        for r in range(row):
            for c in range(col):
                if grid2[r][c] == 1:
                    self.flag = True
                    self.dfs(r, c, grid1, grid2, row, col)
                    if self.flag:
                        num = num + 1
        return num
    def dfs(self, r, c, grid1, grid2, row, col):
        if grid1[r][c] == 1:
            grid2[r][c] = 2
        else:
            self.flag = False
            return
        if r - 1 >= 0 and grid2[r - 1][c] == 1:
            self.dfs(r - 1, c, grid1, grid2, row, col)
        if r + 1 < row and grid2[r + 1][c] == 1:
            self.dfs(r + 1, c, grid1, grid2, row, col)
        if c - 1 >= 0 and grid2[r][c - 1] == 1:
            self.dfs(r, c - 1, grid1, grid2, row, col)
        if c + 1 < col and grid2[r][c + 1] == 1:
            self.dfs(r, c + 1, grid1, grid2, row, col)
self.flag = False 
# 可以在类中的函数公用
# 普通的函数相比,在类中定义的函数只有两点点不同:
# 1、第一个参数永远是 self
# 2、在类中函数相互调用要加 self

执行用时:372 ms, 在所有 Python 提交中击败了100.00%的用户

内存消耗:63.1 MB, 在所有 Python 提交中击败了88.46%的用户


1254.统计封闭岛屿的数目

class Solution(object):
    def closedIsland(self, grid):
        """
        :type grid: List[List[int]]
        :rtype: int
        """
        row = len(grid)
        col = len(grid[0])
        num = 0
        for r in range(row):
            for c in range(col):
                if grid[r][c] == 0:
                    self.flag = True
                    self.dfs(r, c, grid)
                    if self.flag:
                        num = num + 1
        return num
    def dfs(self, r, c, grid):
        # 判断这个岛屿在不在边界,在就False
        if grid[r][c] == 0 and (r == 0 or c == 0 or r == len(grid) - 1 or c == len(grid[0]) - 1):
            self.flag = False
            return
        grid[r][c] = 2
        # 如果0在边界出现 就令flag = False
        if r - 1 >= 0 and  grid[r - 1][c] == 0:
            self.dfs(r - 1, c, grid)
        if r + 1 < len(grid) and grid[r + 1][c] == 0:
            self.dfs(r + 1, c, grid)
        if c - 1 >= 0 and grid[r][c - 1] == 0:
            self.dfs(r, c - 1, grid)
        if c + 1 < len(grid[0]) and grid[r][c + 1] == 0:
            self.dfs(r, c + 1, grid)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值