Skip to content

Commit 3ff8c94

Browse files
Create keys_and_rooms.cpp
1 parent d493059 commit 3ff8c94

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

keys_and_rooms.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
void dfs(vector<vector<int>> &graph, int s, vector<bool> &vis){
3+
vis[s] = true;
4+
for(int i = 0; i < graph[s].size(); i++){
5+
if(!vis[graph[s][i]]){
6+
dfs(graph, graph[s][i], vis);
7+
}
8+
}
9+
}
10+
11+
public:
12+
bool canVisitAllRooms(vector<vector<int>>& rooms) {
13+
int i, n=rooms.size();
14+
vector<bool> vis(n, false);
15+
dfs(rooms, 0, vis);
16+
for(i = 0; i < n; i++){
17+
if(!vis[i]){
18+
return false;
19+
}
20+
}
21+
return true;
22+
}
23+
};

0 commit comments

Comments
 (0)