-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathBS_UnlockRooms.cpp
62 lines (49 loc) · 1.25 KB
/
BS_UnlockRooms.cpp
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
https://binarysearch.com/problems/Unlock-Rooms
Unlock-Rooms
*/
/*
bool solveBFS(vector<vector<int>>& rooms) {
int NumOfRooms = rooms.size();
int countVisited = 0;
int roomIndex=0;
vector<bool> visitedRooms(NumOfRooms, false);
queue<int> q;
q.push(0);
while(!q.empty())
{
roomIndex = q.front(); q.pop();
if(visitedRooms[roomIndex]) continue;
visitedRooms[roomIndex]= true;
countVisited++;
for(int i=0; i<rooms[roomIndex].size(); i++)
{
q.push(rooms[roomIndex][i]);
}
}
return countVisited==NumOfRooms;
}//
*/
int countVisited = 0;
vector<int> visitedRooms;
void recur(int, vector<vector<int>>&);
bool solve(vector<vector<int>>& rooms) {
countVisited=0;
int NumOfRooms = rooms.size();
visitedRooms.clear();
visitedRooms.resize(NumOfRooms, 0);
recur(0, rooms);
// cout<<countVisited<<"- ";
// cout<<NumOfRooms;
return countVisited==NumOfRooms;
}//
void recur(int src, vector<vector<int>>& rooms)
{
// cout<<countVisited<<"fubk "<<visitedRooms[src]<<"cc";
if(visitedRooms[src]) return;
visitedRooms[src]= 1;
countVisited++;
// cout<<countVisited<<"-- ";
for(int r: rooms[src])
recur(r, rooms);
}