leetcode算法---深度优先遍历系列(一)---------694. 不同岛屿的数量

题目描述

给定一个非空01二维数组表示的网格,一个岛屿由四连通(上、下、左、右四个方向)的 1 组成,你可以认为网格的四周被海水包围。

请你计算这个网格中共有多少个形状不同的岛屿。两个岛屿被认为是相同的,当且仅当一个岛屿可以通过平移变换(不可以旋转、翻转)和另一个岛屿重合。

 

样例 1:

11000
11000
00011
00011
给定上图,返回结果 1。

 

样例 2:

11011
10000
00001
11011
给定上图,返回结果 <font color="#c7254e" face="Menlo, Monaco, Consolas, Courier New, monospace">3</font>。

注意:

11
1

 1
11
是不同的岛屿,因为我们不考虑旋转、翻转操作。

 

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/number-of-distinct-islands
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

 

题目分析:

通过尝试优先遍历,找出图中存在的岛屿。还有一个需要解决的问题是如何判断2个岛屿平移后相同,每个岛屿的所有点放入一个列表,每个点用和岛屿开始位置的曼哈顿坐标表示(x0-x_start, y0-y_start),即可以判断2个岛屿是否平移相同。

 

代码实现:

class Solution:
    def numDistinctIslands(self, grid: List[List[int]]) -> int:
            if not grid or not grid[0]:
                return 0

            rows, cols = len(grid), len(grid[0])
            seen = [[0 for _ in range(cols)] for _ in range(rows)]
            dirs = [(0, -1), (0, 1), (-1, 0), (1, 0)]

            def dfs_island(grid, row, col, x_from, y_from, island=None):
                nonlocal seen
                if island is None:
                    island = []

                def is_valid(grid, x, y):
                    rows, cols = len(grid), len(grid[0])
                    if x < 0 or x >= rows:
                        return False

                    if y < 0 or y >= cols:
                        return False

                    return True

                seen[row][col] = 1
                island.append((row - x_from, col - y_from))
                for dir_ in dirs:
                    dir_x, dir_y = dir_
                    x, y = row + dir_x, col + dir_y
                    if is_valid(grid, x, y) and grid[x][y] == 1 and seen[x][y] == 0:
                        dfs_island(grid, x, y, x_from, y_from, island)

                return island

            islands = []
            def add_island(islands, island):
                for ele in islands:
                    if ele == island:
                        return

                islands.append(island)

            for row in range(rows):
                for col in range(cols):
                    if seen[row][col] == 0 and grid[row][col] == 1:
                        add_island(islands, dfs_island(grid, row, col, row, col))
            return len(islands)

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

self-motivation

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值