-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathNearest-Bus-Stop-From-a-House.py
More file actions
57 lines (44 loc) · 1.72 KB
/
Nearest-Bus-Stop-From-a-House.py
File metadata and controls
57 lines (44 loc) · 1.72 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
from enum import Enum
from typing import List, Iterator, Set, Tuple, Deque, DefaultDict
from collections import deque, defaultdict
class CellType(Enum):
EMPTY = 0
WALL = 1
HOUSE = 2
BUS_STOP = 3
class Solution:
def solve(self, matrix: List[List[int]]) -> int:
rows, cols = len(matrix), len(matrix[0]) if matrix else 0
def neighbors(
row: int, col: int, visited: Set[Tuple[int, int]]
) -> Iterator[Tuple[int, int]]:
for neigh_row, neigh_col in (
(row - 1, col),
(row + 1, col),
(row, col - 1),
(row, col + 1),
):
if (
0 <= neigh_row < rows
and 0 <= neigh_col < cols
and (neigh_row, neigh_col) not in visited
and matrix[neigh_row][neigh_col]
not in {CellType.WALL.value, CellType.HOUSE.value}
):
yield (neigh_row, neigh_col)
def bfs() -> int:
queue: Deque[Tuple[int, int, int]] = deque()
for row in range(rows):
for col in range(cols):
if matrix[row][col] == CellType.HOUSE.value:
queue.append((0, row, col))
visited: Set[Tuple[int, int]] = set()
while queue:
distance, row, col = queue.popleft()
visited.add((row, col))
if matrix[row][col] == CellType.BUS_STOP.value:
return distance
for neigh_row, neigh_col in neighbors(row, col, visited):
queue.append((distance + 1, neigh_row, neigh_col))
return -1
return bfs()