diff --git "a/problems/kamacoder/0101.\345\255\244\345\262\233\347\232\204\346\200\273\351\235\242\347\247\257.md" "b/problems/kamacoder/0101.\345\255\244\345\262\233\347\232\204\346\200\273\351\235\242\347\247\257.md" index b300b58ca4..25de67ecd2 100644 --- "a/problems/kamacoder/0101.\345\255\244\345\262\233\347\232\204\346\200\273\351\235\242\347\247\257.md" +++ "b/problems/kamacoder/0101.\345\255\244\345\262\233\347\232\204\346\200\273\351\235\242\347\247\257.md" @@ -186,7 +186,56 @@ int main() { ### Java ### Python - +```python +from collections import deque + +# 处理输入 +n, m = list(map(int, input().strip().split())) +g = [] +for _ in range(n): + row = list(map(int, input().strip().split())) + g.append(row) + +# 定义四个方向、孤岛面积(遍历完边缘后会被重置) +directions = [[0,1], [1,0], [-1,0], [0,-1]] +count = 0 + +# 广搜 +def bfs(r, c): + global count + q = deque() + q.append((r, c)) + g[r][c] = 0 + count += 1 + + while q: + r, c = q.popleft() + for di in directions: + next_r = r + di[0] + next_c = c + di[1] + if next_c < 0 or next_c >= m or next_r < 0 or next_r >= n: + continue + if g[next_r][next_c] == 1: + q.append((next_r, next_c)) + g[next_r][next_c] = 0 + count += 1 + + +for i in range(n): + if g[i][0] == 1: bfs(i, 0) + if g[i][m-1] == 1: bfs(i, m-1) + +for i in range(m): + if g[0][i] == 1: bfs(0, i) + if g[n-1][i] == 1: bfs(n-1, i) + +count = 0 +for i in range(n): + for j in range(m): + if g[i][j] == 1: bfs(i, j) + +print(count) +``` ### Go ### Rust