Go Fib Frog
func fibFrog(a []int) int {
size := len(a)
fib := []int{0, 1}
for i := 1; fib[i] <= size; {
i++
fib = append(fib, fib[i-1]+fib[i-2])
}
type path struct {
idx int
jmp int
}
paths := []path{{idx: -1, jmp: 0}}
steps := make([]bool, size)
for len(paths) > 0 {
cur := paths[0]
paths = paths[1:]
for i := len(fib) - 1; i >= 2; i-- {
idx := cur.idx + fib[i]
if idx == size {
return cur.jmp + 1
}
if idx > size || steps[idx] || a[idx] == 0 {
continue
}
if a[idx] == 1 {
steps[idx] = true
paths = append(paths, path{idx: idx, jmp: cur.jmp + 1})
}
}
}
return -1
}
This precomputes Fibonacci jumps, then uses a breadth-first search to find the shortest valid path across the river.