diff --git a/problems/0695.岛屿的最大面积.md b/problems/0695.岛屿的最大面积.md index e1f43e36..447a374c 100644 --- a/problems/0695.岛屿的最大面积.md +++ b/problems/0695.岛屿的最大面积.md @@ -170,6 +170,81 @@ public: # 其它语言版本 +## Python +### BFS +```python +class Solution: + def __init__(self): + self.count = 0 + + def maxAreaOfIsland(self, grid: List[List[int]]) -> int: + # 与200.独立岛屿不同的是:此题grid列表内是int!!! + + # BFS + if not grid: return 0 + + m, n = len(grid), len(grid[0]) + visited = [[False for i in range(n)] for j in range(m)] + + result = 0 + for i in range(m): + for j in range(n): + if not visited[i][j] and grid[i][j] == 1: + # 每一个新岛屿 + self.count = 0 + print(f'{self.count}') + self.bfs(grid, visited, i, j) + result = max(result, self.count) + + return result + + def bfs(self, grid, visited, i, j): + self.count += 1 + visited[i][j] = True + + queue = collections.deque([(i, j)]) + while queue: + x, y = queue.popleft() + for new_x, new_y in [(x + 1, y), (x - 1, y), (x, y - 1), (x, y + 1)]: + if 0 <= new_x < len(grid) and 0 <= new_y < len(grid[0]) and not visited[new_x][new_y] and grid[new_x][new_y] == 1: + visited[new_x][new_y] = True + self.count += 1 + queue.append((new_x, new_y)) +``` +### DFS +```python +class Solution: + def __init__(self): + self.count = 0 + + def maxAreaOfIsland(self, grid: List[List[int]]) -> int: + # DFS + if not grid: return 0 + + m, n = len(grid), len(grid[0]) + visited = [[False for _ in range(n)] for _ in range(m)] + + result = 0 + for i in range(m): + for j in range(n): + if not visited[i][j] and grid[i][j] == 1: + self.count = 0 + self.dfs(grid, visited, i, j) + result = max(result, self.count) + return result + + def dfs(self, grid, visited, x, y): + if visited[x][y] or grid[x][y] == 0: + return + visited[x][y] = True + self.count += 1 + for new_x, new_y in [(x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1)]: + if 0 <= new_x < len(grid) and 0 <= new_y < len(grid[0]): + self.dfs(grid, visited, new_x, new_y) +``` + + + ## Java 这里使用深度优先搜索 DFS 来完成本道题目。我们使用 DFS 计算一个岛屿的面积,同时维护计算过的最大的岛屿面积。同时,为了避免对岛屿重复计算,我们在 DFS 的时候对岛屿进行 “淹没” 操作,即将岛屿所占的地方置为 0。 @@ -199,4 +274,4 @@ public int dfs(int[][] grid,int i,int j){ dfs(grid,i,j + 1) + dfs(grid,i,j - 1); } -``` \ No newline at end of file +```