841. Keys and Rooms

PHOTO EMBED

Sun Mar 19 2023 08:02:15 GMT+0000 (Coordinated Universal Time)

Saved by @Ranjan_kumar #c++

class Solution {
public:
    bool canVisitAllRooms(vector<vector<int>>& rooms) {
        int n=rooms.size();
        vector<bool> visited(n, 0);
        queue<int>q;
        q.push(0);
        visited[0]=1;
        while(!q.empty())
        {
            int p=q.front();
            q.pop();
            
            for(auto x:rooms[p])
            {
                if(!visited[x])
                {
                    q.push(x);
                    visited[x]=1;
                }
            }
        }
        bool g=true;
        for(auto x:visited)
        {
            if(!x) g=false;
        }
        return g;
    }
};
content_copyCOPY

https://leetcode.com/problems/keys-and-rooms/