1. 程式人生 > >leetcode Keys and Rooms

leetcode Keys and Rooms

簡單圖搜尋

class Solution {
public:
    bool canVisitAllRooms(vector<vector<int>>& room) {
        bool r[room.size()];
        for(int i=0;i<room.size();i++)
            r[i]=false;
        queue<int> q;
        q.push(0);
        r[0]=true;
        while(!q.empty()){
            int f=q.front();
            q.pop();
            for(int i=0;i<room[f].size();i++){
                if(!r[room[f][i]]){
                    r[room[f][i]]=true;
                    q.push(room[f][i]);
                }
            }
        }
        for(int i=0;i<room.size();i++)
            if(!r[i])
                return false;
        return true;
    }
};