给定一个包含了一些 0 和 1 的非空二维数组 grid 。
一个 岛屿 是由一些相邻的 1 (代表土地) 构成的组合,这里的「相邻」要求两个 1 必须在水平或者竖直方向上相邻。你可以假设 grid 的四个边缘都被 0(代表水)包围着。
找到给定的二维数组中最大的岛屿面积。(如果没有岛屿,则返回面积为 0 。)
示例 1:
[[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]]
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/max-area-of-island
# 深度优先搜索
class Solution:
def dfs(self, grid, i, j):
if i < 0 or i >= len(grid) or j < 0 or j >= len(grid[0]) or grid[i][j] != 1:
return 0
grid[i][j] = 0
ans = 1
for di, dj in [[0, 1], [0, -1], [1, 0], [-1, 0]]:
nexti, nextj = i+di, j+dj
ans += self.dfs(grid, nexti, nextj)
return ans
def maxAreaOfIsland(self, grid):
ans = 0
for i, l in enumerate(grid):
for j, n in enumerate(l):
ans = max(ans, self.dfs(grid, i, j))
return ans
# 深度优先搜索 + 栈
class Solution:
def maxAreaOfIsland(self, grid):
ans = 0
for i, l in enumerate(grid):
for j, n in enumerate(l):
cur = 0
stack = [(i, j)]
while stack:
cur_i, cur_j = stack.pop()
if cur_i < 0 or cur_j < 0 or cur_i == len(grid) or cur_j == len(grid[0]) or grid[cur_i][cur_j] != 1:
continue
cur += 1
grid[cur_i][cur_j] = 0
for di, dj in [[0, 1], [0, -1], [1, 0], [-1, 0]]:
next_i, next_j = cur_i+di, cur_j+dj
stack.append((next_i, next_j))
ans = max(ans, cur)
return ans
# 广度优先搜索
import collections
class Solution:
def maxAreaOfIsland(self, grid):
ans = 0
for i, l in enumerate(grid):
for j, n in enumerate(l):
cur = 0
q = collections.deque([(i, j)])
while q:
cur_i, cur_j = q.popleft()
if cur_i < 0 or cur_j < 0 or cur_i == len(grid) or cur_j == len(grid[0]) or grid[cur_i][cur_j] != 1:
continue
cur += 1
grid[cur_i][cur_j] = 0
for di, dj in [[0, 1], [0, -1], [1, 0], [-1, 0]]:
next_i, next_j = cur_i + di, cur_j + dj
q.append((next_i, next_j))
ans = max(ans, cur)
return ans