From 0f85ea546c5f9205937591f3ae1e940262af985d Mon Sep 17 00:00:00 2001
From: bt3gl <138340846+bt3gl-cryptographer@users.noreply.github.com>
Date: Tue, 8 Aug 2023 16:18:57 -0700
Subject: [PATCH] Update README.md
---
graphs/README.md | 53 ++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 53 insertions(+)
diff --git a/graphs/README.md b/graphs/README.md
index 87ae04b..0a3d6e7 100644
--- a/graphs/README.md
+++ b/graphs/README.md
@@ -208,3 +208,56 @@ def iterative_dfs(graph, v, discovered):
stack.append(u)
```
+
+
+---
+
+### matrix bfs
+
+
+
+* given an `m x n` grid rooms initialized with these three possible values:
+ * -1 A wall or an obstacle.
+ * 0 A gate.
+ * `INF` Infinity means an empty room (`2^31 - 1 = 2147483647` to represent `INF`)
+
+* fill empty room with the distance to its nearest gate. if it is impossible to reach a gate, it should be filled with `INF`.
+
+
+```python
+
+
+def matrix_bfs(rooms) -> None:
+
+ m = len(rooms)
+ if m == 0:
+ return rooms
+ n = len(rooms[0])
+
+ GATE = 0
+ EMPTY = 2147483647
+ DIRECTIONS = ((1, 0), (-1, 0), (0, 1), (0, -1))
+
+ queue = collections.deque()
+
+ for i in range(m):
+ for j in range(n):
+
+ if rooms[i][j] == GATE:
+ q.append((i, j))
+
+ while queue:
+
+ row, col = queue.popleft()
+
+ for (x, y) in DIRECTIONS:
+
+ r = row + x
+ c = col + y
+
+ if (r < 0) or (c < 0) or (r >= m) or (c >= n) or rooms[r][c] != EMPTY:
+ continue
+
+ rooms[r][c] = rooms[row][col] + 1
+ queue.append((r, c))
+```