-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbfs.py
More file actions
137 lines (99 loc) · 2.71 KB
/
bfs.py
File metadata and controls
137 lines (99 loc) · 2.71 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
from collections import deque
from typing import List, Deque, Tuple
def findCircleNum(isConnected: List[List[int]]) -> int:
n = len(isConnected)
visited = [False] * n
provinces = 0
def visit_all_connected_nodes(city: int) -> None:
queue = deque([city])
visited[city] = True
while queue:
node = queue.popleft()
for related_city in range(n):
if isConnected[node][related_city] and not visited[related_city]:
visited[related_city] = True
queue.append(related_city)
for city in range(n):
if not visited[city]:
provinces += 1
visit_all_connected_nodes(city)
return provinces
#funciona mas com baixo desempenho
def numIslands(grid: List[List[str]]) -> int:
if not grid:
return 0
m = len(grid)
n = len(grid[0])
visited = set()
islands = 0
def visit_island_nodes(point: tuple) -> None:
queue = deque([point])
while queue:
point = queue.popleft()
x, y = point[0], point[1]
if point not in visited:
visited.add(point)
if grid[x][y] == '1':
if x+1 < m:
queue.append((x+1, y))
if y+1 < n:
queue.append((x, y+1))
if x-1 >= 0:
queue.append((x-1, y))
if y-1 >= 0:
queue.append((x, y-1))
for x in range(m):
for y in range(n):
point = (x, y)
if grid[x][y] == '1' and point not in visited:
islands += 1
visit_island_nodes(point)
return islands
#versão otimizada, sem usar set "colorindo" a própria matriz e mais legível
def numIslandsV2(grid: List[List[str]]) -> int:
if not grid:
return 0
m = len(grid)
n = len(grid[0])
islands = 0
directions = ((1, 0), (-1, 0), (0, 1), (0, -1))
def visit_linked_islands(x: int, y: int) -> None:
queue: Deque[Tuple[int, int]] = deque([(x, y)])
while queue:
x, y = queue.popleft()
for dx, dy in directions:
nx, ny = x + dx, y + dy
valid_x = 0 <= nx < m
valid_y = 0 <= ny < n
if valid_x and valid_y and grid[nx][ny] == '1':
grid[nx][ny] = '0'
queue.append((nx, ny))
for x in range(m):
for y in range(n):
if grid[x][y] != '1':
continue
islands += 1
grid[x][y] = '0'
visit_linked_islands(x, y)
return islands
def floodFill(image: List[List[int]], sr: int, sc: int, color: int) -> List[List[int]]:
if not image:
return [[]]
color_to_overwrite = image[sr][sc]
if color_to_overwrite == color:
return image
image[sr][sc] = color
m = len(image)
n = len(image[0])
directions = ((0, 1), (1, 0), (0, -1), (-1, 0))
queue = deque([(sr, sc)])
while queue:
x, y = queue.popleft()
for dx, dy in directions:
nx, ny = x + dx, y + dy
valid_x = 0 <= nx < m
valid_y = 0 <= ny < n
if valid_x and valid_y and image[nx][ny] == color_to_overwrite:
image[nx][ny] = color
queue.append((nx, ny))
return image