-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0841-Keys-and-rooms.cs
More file actions
44 lines (34 loc) · 1.13 KB
/
0841-Keys-and-rooms.cs
File metadata and controls
44 lines (34 loc) · 1.13 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
using System;
using System.Collections.Generic;
using System.Text;
namespace Solution._0841.Keys_and_rooms
{
public class _0841_Keys_and_rooms
{
public bool CanVisitAllRooms(IList<IList<int>> rooms)
{
if (rooms.Count <= 1) return true;
// record whether the room is open.
Dictionary<int, bool> dic = new Dictionary<int, bool>();
for (int i = 0; i < rooms.Count; i++)
dic.Add(i, false);
Queue<int> keys = new Queue<int>();
keys.Enqueue(0);
while (keys.Count > 0)
{
// open the room with key.
int key = keys.Dequeue();
if (dic[key]) continue;
// pick up the key.
for (int i = 0; i < rooms[key].Count; i++)
keys.Enqueue(rooms[key][i]);
// record the room is opened.
dic[key] = true;
// check whether all room is open.
if (dic.ContainsValue(false)) continue;
else return true;
}
return false;
}
}
}