aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--841/go.mod3
-rw-r--r--841/main.go26
2 files changed, 29 insertions, 0 deletions
diff --git a/841/go.mod b/841/go.mod
new file mode 100644
index 0000000..d38aff7
--- /dev/null
+++ b/841/go.mod
@@ -0,0 +1,3 @@
+module 841
+
+go 1.19
diff --git a/841/main.go b/841/main.go
new file mode 100644
index 0000000..586f0fd
--- /dev/null
+++ b/841/main.go
@@ -0,0 +1,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}, {}}))
+}