Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 655a513

Browse files
841 python3 add BFS solution.md
1 parent f3d836d commit 655a513

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

‎problems/0841.钥匙和房间.md‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ class Solution {
324324
### python3
325325

326326
```python
327-
327+
# 深度搜索优先
328328
class Solution:
329329
def dfs(self, key: int, rooms: List[List[int]] , visited : List[bool] ) :
330330
if visited[key] :
@@ -346,6 +346,31 @@ class Solution:
346346
return False
347347
return True
348348

349+
# 广度搜索优先
350+
class Solution:
351+
def canVisitAllRooms(self, rooms: List[List[int]]) -> bool:
352+
visited = [False] * len(rooms)
353+
self.bfs(rooms, 0, visited)
354+
355+
for room in visited:
356+
if room == False:
357+
return False
358+
359+
return True
360+
361+
def bfs(self, rooms, index, visited):
362+
q = collections.deque()
363+
q.append(index)
364+
365+
visited[0] = True
366+
367+
while len(q) != 0:
368+
index = q.popleft()
369+
for nextIndex in rooms[index]:
370+
if visited[nextIndex] == False:
371+
q.append(nextIndex)
372+
visited[nextIndex] = True
373+
349374
```
350375

351376

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /