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
|
package main
import "fmt"
func canVisitAllRooms(rooms [][]int) bool {
visited := map[int]bool{}
dfs(rooms, visited, 0)
return len(visited) == len(rooms)
}
func dfs(rooms [][]int, visited map[int]bool, room int) {
if _, ok := visited[room]; !ok {
visited[room] = true
} else {
return
}
for i := range rooms[room] {
dfs(rooms, visited, rooms[room][i])
}
}
func main() {
fmt.Println(canVisitAllRooms([][]int{{1}, {2}, {3}, {}}))
fmt.Println(canVisitAllRooms([][]int{{1, 3}, {3, 0, 1}, {2}, {}}))
}
|