Skip to content

Instantly share code, notes, and snippets.

@s4553711
Created May 30, 2018 13:23
Show Gist options
  • Save s4553711/05b85d622603cf915bd2e688979f30a2 to your computer and use it in GitHub Desktop.
Save s4553711/05b85d622603cf915bd2e688979f30a2 to your computer and use it in GitHub Desktop.
class Solution {
public:
bool canVisitAllRooms(vector<vector<int>>& rooms) {
int numrooms = rooms.size();
vector<bool> visited(numrooms, false);
bool res = false;
visited = dfs(rooms, 0, visited);
for(int i = 0; i < numrooms; i++) {
if (!visited[i]) return false;
}
return true;
}
vector<bool> dfs(vector<vector<int>>& rooms, int room, vector<bool> visited) {
visited[room] = true;
for(int nextroom : rooms[room]) {
if (visited[nextroom] == false) visited = dfs(rooms, nextroom, visited);
}
return visited;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment