aboutsummaryrefslogtreecommitdiff
path: root/keys_and_rooms.rs
blob: 678c732fa19f658f2a96201aaf80854132ff754d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
struct Solution;

impl Solution {
    pub fn can_visit_all_rooms(rooms: Vec<Vec<i32>>) -> bool {
        let mut unlocked = vec![false; rooms.len()];
        unlocked[0] = true;
        let mut stack = vec![0];
        while let Some(next) = stack.pop() {
            for &i in &rooms[next as usize] {
                if !unlocked[i as usize] {
                    unlocked[i as usize] = true;
                    stack.push(i);
                }
            }
        }
        unlocked.iter().all(|&b| b)
    }
}

fn main() {
    let rooms = vec![vec![1], vec![2], vec![3], vec![]];
    print!("{}", Solution::can_visit_all_rooms(rooms));
}