From 885550225720f1141a80ca6dc7ea550bba20afa8 Mon Sep 17 00:00:00 2001 From: terminaldweller Date: Tue, 20 Dec 2022 08:47:57 +0330 Subject: 841 --- 841/go.mod | 3 +++ 841/main.go | 26 ++++++++++++++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 841/go.mod create mode 100644 841/main.go (limited to '841') 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}, {}})) +} -- cgit v1.2.3