mirror of
https://github.moeyy.xyz/https://github.com/TheAlgorithms/C.git
synced 2023-10-11 15:56:24 +08:00
103361de54
Co-authored-by: David Leal <halfpacho@gmail.com>
28 lines
792 B
C
28 lines
792 B
C
void visitRooms(int key, int** rooms, int roomsSize, int* roomsColSize, int* visitedRooms){
|
|
if (visitedRooms[key] == 1){
|
|
return;
|
|
}
|
|
|
|
visitedRooms[key] = 1;
|
|
for (int i = 0; i < roomsColSize[key]; i++){
|
|
visitRooms(rooms[key][i], rooms, roomsSize, roomsColSize, visitedRooms);
|
|
}
|
|
}
|
|
|
|
// Depth-first search
|
|
// Runtime: O(n)
|
|
// Space: O(n)
|
|
bool canVisitAllRooms(int** rooms, int roomsSize, int* roomsColSize){
|
|
int* visitedRooms = calloc(roomsSize, sizeof(int));
|
|
visitRooms(0, rooms, roomsSize, roomsColSize, visitedRooms);
|
|
|
|
int visitedRoomsNumber = 0;
|
|
for (int i = 0; i < roomsSize; i++){
|
|
if (visitedRooms[i] == 1){
|
|
visitedRoomsNumber++;
|
|
}
|
|
}
|
|
|
|
return visitedRoomsNumber == roomsSize;
|
|
}
|