diff --git a/problems/kamacoder/0104.建造最大岛屿.md b/problems/kamacoder/0104.建造最大岛屿.md index 7d67b7fc..7132217b 100644 --- a/problems/kamacoder/0104.建造最大岛屿.md +++ b/problems/kamacoder/0104.建造最大岛屿.md @@ -366,6 +366,79 @@ public class Main { ### Python +```Python +import collections + +directions = [[-1, 0], [0, 1], [0, -1], [1, 0]] +area = 0 + +def dfs(i, j, grid, visited, num): + global area + + if visited[i][j]: + return + + visited[i][j] = True + grid[i][j] = num # 标记岛屿号码 + area += 1 + + for x, y in directions: + new_x = i + x + new_y = j + y + if ( + 0 <= new_x < len(grid) + and 0 <= new_y < len(grid[0]) + and grid[new_x][new_y] == "1" + ): + dfs(new_x, new_y, grid, visited, num) + + +def main(): + global area + + N, M = map(int, input().strip().split()) + grid = [] + for i in range(N): + grid.append(input().strip().split()) + visited = [[False] * M for _ in range(N)] + rec = collections.defaultdict(int) + + cnt = 2 + for i in range(N): + for j in range(M): + if grid[i][j] == "1": + area = 0 + dfs(i, j, grid, visited, cnt) + rec[cnt] = area # 纪录岛屿面积 + cnt += 1 + + res = 0 + for i in range(N): + for j in range(M): + if grid[i][j] == "0": + max_island = 1 # 将水变为陆地,故从1开始计数 + v = set() + for x, y in directions: + new_x = i + x + new_y = j + y + if ( + 0 <= new_x < len(grid) + and 0 <= new_y < len(grid[0]) + and grid[new_x][new_y] != "0" + and grid[new_x][new_y] not in v # 岛屿不可重复 + ): + max_island += rec[grid[new_x][new_y]] + v.add(grid[new_x][new_y]) + res = max(res, max_island) + + if res == 0: + return max(rec.values()) # 无水的情况 + return res + +if __name__ == "__main__": + print(main()) +``` + ### Go ### Rust