(待更..)【Leetcode 深搜、广搜、并查集】岛屿数量(200)

题目

给定一个由 '1'(陆地)和 '0'(水)组成的的二维网格,计算岛屿的数量。一个岛被水包围,并且它是通过水平方向或垂直方向上相邻的陆地连接而成的。你可以假设网格的四个边均被水包围。

示例 1:

输入:
11110
11010
11000
00000

输出: 1

示例 2:

输入:
11000
11000
00100
00011

输出: 3

解答

典型的搜索题,深度优先和广度优先都可以。(并查集也可以解决,待更..)
最坏情况:全部都是陆地,需要m*n的函数调用栈空间。

1,深度优先dfs,用递归。Time: O(mn),Space:O(mn)
2,广度优先bfs,用队列。Time: O(mn),Space:O(mn)

代码实现:

# 深度优先搜索
class Solution:
    def __init__(self):
        self.next = [
            [0, 1],
            [1, 0],
            [0, -1],
            [-1, 0]
        ]
        self.m = 0
        self.n = 0
        self.a = [[]]

    def numIslands(self, grid) -> int:
        if not grid:
            return 0

        self.a = grid
        self.m, self.n = len(self.a), len(self.a[0])
        color = 0

        for i in range(self.m):
            for j in range(self.n):
                if self.a[i][j] == '1':
                    color -= 1
                    self.a[i][j] = color
                    self.dfs(i, j, color)
        return -color

    def dfs(self, x, y, color):
        for i in range(4):
            tx = x + self.next[i][0]
            ty = y + self.next[i][1]
            if tx >= 0 and ty >= 0 and tx < self.m and ty < self.n and self.a[tx][ty] == '1':
                self.a[tx][ty] = color
                self.dfs(tx, ty, color)


s = Solution()
ans = s.numIslands([
    ['1', '1', '0', '0', '0'],
    ['1', '1', '0', '0', '0'],
    ['0', '0', '1', '0', '0'],
    ['0', '0', '0', '1', '1']
])
print(ans)

import pprint
pprint.pprint(s.a)

# 3
[[-1, -1, '0', '0', '0'],
 [-1, -1, '0', '0', '0'],
 ['0', '0', -2, '0', '0'],
 ['0', '0', '0', -3, -3]]

# 广度优先搜索

class Queue:  # 队列结构
    def __init__(self):
        self.x = -1
        self.y = -1


class Solution:
    def __init__(self):
        self.next = [
            [0, 1],
            [1, 0],
            [0, -1],
            [-1, 0]
        ]
        self.m = 0
        self.n = 0
        self.a = [[]]
        self.que = []

    def numIslands(self, grid) -> int:
        if not grid:
            return 0

        self.a = grid
        self.m, self.n = len(self.a), len(self.a[0])
        self.que = [Queue() for _ in range(self.m*self.n)]
        color = 0  # 填涂独立小岛

        for i in range(self.m):
            for j in range(self.n):
                if self.a[i][j] == '1':
                    color -= 1
                    head = tail = 0
                    self.que[tail].x = i
                    self.que[tail].y = j
                    tail += 1
                    self.a[i][j] = color
                    self.bfs(color, head, tail)
        return -color

    def bfs(self, color, head, tail):
        while head < tail:
            for i in range(4):
                tx = self.que[head].x + self.next[i][0]
                ty = self.que[head].y + self.next[i][1]
                if tx >= 0 and ty >= 0 and tx < self.m and ty < self.n and self.a[tx][ty] == '1':
                    self.que[tail].x = tx
                    self.que[tail].y = ty
                    tail += 1
                    self.a[tx][ty] = color
            head += 1


s = Solution()
ans = s.numIslands([
    ['1', '1', '0', '0', '0'],
    ['1', '1', '0', '0', '0'],
    ['0', '0', '1', '0', '0'],
    ['0', '0', '0', '1', '1']
])
print(ans)

import pprint
pprint.pprint(s.a)

# 3
[[-1, -1, '0', '0', '0'],
 [-1, -1, '0', '0', '0'],
 ['0', '0', -2, '0', '0'],
 ['0', '0', '0', -3, -3]]
原文地址:https://www.cnblogs.com/ldy-miss/p/12176436.html