-
Notifications
You must be signed in to change notification settings - Fork 0
/
200-number-of-islands.py
58 lines (52 loc) · 1.84 KB
/
200-number-of-islands.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
"""
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands.
An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically.
You may assume all four edges of the grid are all surrounded by water.
Example:
Input:
11110
11010
11000
00000
Output: 1
"""
import collections
class Solution:
def numIslands(self, grid):
"""
:type grid: List[List[str]]
:rtype: int
"""
if not grid:
return 0
queue = collections.deque([])
h = len(grid)
w = len(grid[0])
cnt = 0
for i in range(w):
for j in range(h):
if grid[j][i] == '1':
# replace '1' with '-' to represent visited land
grid[j][i] = '-'
queue.append([i, j])
cnt += 1
# BFS
while queue:
x, y = queue.popleft()
if x > 0 and grid[y][x - 1] == '1':
grid[y][x - 1] = '-'
queue.append([x - 1, y])
if x < w - 1 and grid[y][x + 1] == '1':
grid[y][x + 1] = '-'
queue.append([x + 1, y])
if y > 0 and grid[y - 1][x] == '1':
grid[y - 1][x] = '-'
queue.append([x, y - 1])
if y < h - 1 and grid[y + 1][x] == '1':
grid[y + 1][x] = '-'
queue.append([x, y + 1])
return cnt
if __name__ == "__main__":
grid = [["1", "1", "1", "1", "0"], ["1", "1", "0", "1", "0"],
["1", "1", "0", "0", "0"], ["0", "0", "0", "0", "0"]]
print(Solution().numIslands(grid))