1
0
Fork 0

Fix infinite loop

Co-authored-by: David Schlachter <https://github.com/davidschlachter>
main
Bill Ewanick 2023-11-15 17:32:10 -05:00
parent f9192bc541
commit 262f2aae90
1 changed files with 5 additions and 3 deletions

View File

@ -3,7 +3,7 @@ package main
import "fmt" import "fmt"
func main() { func main() {
res := canVisitAllRooms([][]int{{1}, {2}, {}}) res := canVisitAllRooms([][]int{{1, 3}, {3, 0, 1}, {2}, {0}})
fmt.Printf("%+v\n", res) fmt.Printf("%+v\n", res)
} }
@ -27,11 +27,13 @@ func getAllKeys(index int, seen map[int]struct{}, rooms [][]int) map[int]struct{
keys := map[int]struct{}{} keys := map[int]struct{}{}
for j := 0; j < len(rooms[index]); j++ { for j := 0; j < len(rooms[index]); j++ {
if _, ok := keys[rooms[index][j]]; ok { if _, ok := seen[rooms[index][j]]; ok {
continue continue
} else {
seen[rooms[index][j]] = struct{}{}
} }
keys[rooms[index][j]] = struct{}{} keys[rooms[index][j]] = struct{}{}
insideKeys := getAllKeys(j, keys, rooms) insideKeys := getAllKeys(rooms[index][j], seen, rooms)
for k := range insideKeys { for k := range insideKeys {
keys[k] = struct{}{} keys[k] = struct{}{}
} }