## 841. Keys and Rooms

There are `N`

rooms and you start in room `0`

. Each room has a distinct number in `0, 1, 2, ..., N-1`

, and each room may have some keys to access the next room.

Formally, each room `i`

has a list of keys `rooms[i]`

, and each key `rooms[i][j]`

is an integer in `[0, 1, ..., N-1]`

where `N = rooms.length`

. A key `rooms[i][j] = v`

opens the room with number `v`

.

Initially, all the rooms start locked (except for room `0`

).

You can walk back and forth between rooms freely.

Return `true`

if and only if you can enter every room.

**Example 1:**

Input:[[1],[2],[3],[]]Output:trueExplanation:We start in room 0, and pick up key 1. We then go to room 1, and pick up key 2. We then go to room 2, and pick up key 3. We then go to room 3. Since we were able to go to every room, we return true.

**Example 2:**

Input:[[1,3],[3,0,1],[2],[0]]Output:falseExplanation:We can't enter the room with number 2.

**Note:**

`1 <= rooms.length <= 1000`

`0 <= rooms[i].length <= 1000`

- The number of keys in all rooms combined is at most
`3000`

.

## Rust Solution

```
struct Solution;
use std::collections::HashSet;
use std::collections::VecDeque;
impl Solution {
fn can_visit_all_rooms(rooms: Vec<Vec<i32>>) -> bool {
let n = rooms.len();
let mut visited: HashSet<usize> = HashSet::new();
let mut queue: VecDeque<usize> = VecDeque::new();
queue.push_back(0);
while let Some(front) = queue.pop_front() {
let room = &rooms[front];
if visited.insert(front) {
for &key in room {
queue.push_back(key as usize);
}
}
}
visited.len() == n
}
}
#[test]
fn test() {
let rooms = vec_vec_i32![[1], [2], [3], []];
let res = true;
assert_eq!(Solution::can_visit_all_rooms(rooms), res);
let rooms = vec_vec_i32![[1, 3], [3, 0, 1], [2], [0]];
let res = false;
assert_eq!(Solution::can_visit_all_rooms(rooms), res);
}
```

Having problems with this solution? Click here to submit an issue on github.