diff --git "a/problems/0200.345円262円233円345円261円277円346円225円260円351円207円217円.345円271円277円346円220円234円347円211円210円.md" "b/problems/0200.345円262円233円345円261円277円346円225円260円351円207円217円.345円271円277円346円220円234円347円211円210円.md" index 39af9f50a3..c20fe4f1aa 100644 --- "a/problems/0200.345円262円233円345円261円277円346円225円260円351円207円217円.345円271円277円346円220円234円347円211円210円.md" +++ "b/problems/0200.345円262円233円345円261円277円346円225円260円351円207円217円.345円271円277円346円220円234円347円211円210円.md" @@ -196,7 +196,51 @@ class Solution { } } ``` + +## 其他语言版本 +### Python +BFS solution +```python +class Solution: + def __init__(self): + self.dirs = [[0, 1], [1, 0], [-1, 0], [0, -1]] + + def numIslands(self, grid: List[List[str]]) -> int: + m = len(grid) + n = len(grid[0]) + visited = [[False]*n for _ in range(m)] + res = 0 + for i in range(m): + for j in range(n): + if visited[i][j] == False and grid[i][j] == '1': + res += 1 + self.bfs(grid, i, j, visited) # Call bfs within this condition + return res + + def bfs(self, grid, i, j, visited): + q = deque() + q.append((i,j)) + visited[i][j] = True + while q: + x, y = q.popleft() + for k in range(4): + next_i = x + self.dirs[k][0] + next_j = y + self.dirs[k][1] + + if next_i < 0 or next_i>= len(grid): + continue + if next_j < 0 or next_j>= len(grid[0]): + continue + if visited[next_i][next_j]: + continue + if grid[next_i][next_j] == '0': + continue + q.append((next_i, next_j)) + visited[next_i][next_j] = True +``` +