463. 岛屿的周长

链接

https://leetcode.cn/problems/island-perimeter/description/

思路

这题理论上来讲可以用深搜广搜来做,但我第一时间没搞明白怎么做,所以就先迭代一发。

思路就是:

1. 题目给定的只有1个岛屿,那么我们可以遍历整个grid,对于发现的新岛屿,先按其没有相邻来处理(+4)。然后看一下他周边是否有其他岛屿,如果有的话就-1.

2. 同样的思路,我可以写成深搜。

3. 同样的思路,我可以写成广搜。

代码一

class Solution:
    def islandPerimeter(self, grid) -> int:
        height = len(grid)
        if height < 1:
            return 0
        width = len(grid[0])
        if width < 1:
            return 0
        direct_x = [0, -1, 0, 1]
        direct_y = [-1, 0, 1, 0]
        res = 0
        for i in range(height):
            for j in range(width):
                if grid[i][j] == 1:
                    res += 4
                    for k in range(4):
                        new_x, new_y = i + direct_x[k], j + direct_y[k]
                        if 0 <= new_x < height and 0 <= new_y < width and grid[new_x][new_y] == 1:
                            res -= 1
        return res

代码二

class Solution:
    def islandPerimeter(self, grid) -> int:
        height = len(grid)
        if height < 1:
            return 0
        width = len(grid[0])
        if width < 1:
            return 0
        visited = [[False for i in range(width)] for i in range(height)]
        res = 0
        for i in range(height):
            for j in range(width):
                if not visited[i][j] and grid[i][j] == 1:
                    res += self.dfs(i, j, grid, visited)
        return res

    def dfs(self, x, y, grid, visited):
        if visited[x][y]:
            return 0
        visited[x][y] = True
        res = 4
        direct_x = [-1, 0, 1, 0]
        direct_y = [0, -1, 0, 1]
        for i in range(4):
            target_x, target_y = x + direct_x[i], y + direct_y[i]
            if 0 <= target_x < len(grid) and 0 <= target_y < len(grid[0]) and grid[target_x][target_y] == 1:
                res -= 1
                if not visited[target_x][target_y]:
                    res += self.dfs(target_x, target_y, grid, visited)
        return res


if __name__ == '__main__':
    s = Solution()
    print(s.islandPerimeter([[0, 1, 0, 0], [1, 1, 1, 0], [0, 1, 0, 0], [1, 1, 0, 0]]))

代码三:

class Solution:
    def islandPerimeter(self, grid) -> int:
        height = len(grid)
        if height < 1:
            return 0
        width = len(grid[0])
        if width < 1:
            return 0
        visited = [[False for i in range(width)] for i in range(height)]
        res = 0
        for i in range(height):
            for j in range(width):
                if not visited[i][j] and grid[i][j] == 1:
                    res += self.bfs(i, j, grid, visited)
        return res

    def bfs(self, x, y, grid, visited):
        if visited[x][y]:
            return 0
        visited[x][y] = True
        res = 4
        direct_x = [-1, 0, 1, 0]
        direct_y = [0, -1, 0, 1]
        next_pool = []
        for i in range(4):
            target_x, target_y = x + direct_x[i], y + direct_y[i]
            if 0 <= target_x < len(grid) and 0 <= target_y < len(grid[0]) and grid[target_x][target_y] == 1:
                res -= 1
                if not visited[target_x][target_y]:
                    next_pool.append((target_x, target_y))
        for next_x, next_y in next_pool:
            res += self.bfs(next_x, next_y, grid, visited)
        return res


if __name__ == '__main__':
    s = Solution()
    print(s.islandPerimeter([[0, 1, 0, 0], [1, 1, 1, 0], [0, 1, 0, 0], [1, 1, 0, 0]]))

 代码四

class Solution:
    def islandPerimeter(self, grid) -> int:
        if not grid and not grid[0]:
            return 0
        height, width = len(grid), len(grid[0])
        visited = [[False for i in range(width)] for j in range(height)]
        direct_x = [-1, 0, 1, 0]
        direct_y = [0, -1, 0, 1]

        def dfs(x, y):
            if x < 0 or y < 0 or x >= height or y >= width or visited[x][y] or grid[x][y] == 0:
                return 0
            res = 4
            visited[x][y] = True
            for i in range(4):
                next_x, next_y = x + direct_x[i], y + direct_y[i]
                if 0 <= next_x < height and 0 <= next_y < width and grid[next_x][next_y] == 1:
                    res -= 1
                    if not visited[next_x][next_y]:
                        res += dfs(next_x, next_y)
            return res

        for i in range(height):
            for j in range(width):
                if grid[i][j] == 1:
                    return dfs(i, j)

 

posted @ 2023-09-04 13:40  BJFU-VTH  阅读(5)  评论(0编辑  收藏  举报