Bash Fib Frog
fib_frog() {
local -n _arr="$1"
local _size=${#_arr[@]}
local -a _fib=(0 1)
local _i=1
while (( _fib[_i] <= _size )); do
((_i++))
_fib[_i]=$(( _fib[_i-1] + _fib[_i-2] ))
done
local -a _q_idx=(-1) _q_jmp=(0)
local _head=0
local -a _steps
for ((_i = 0; _i < _size; _i++)); do _steps[_i]=0; done
while (( _head < ${#_q_idx[@]} )); do
local _cidx=${_q_idx[_head]} _cjmp=${_q_jmp[_head]}
((_head++))
local _fidx
for ((_fidx = ${#_fib[@]} - 1; _fidx >= 2; _fidx--)); do
local _idx=$(( _cidx + _fib[_fidx] ))
if (( _idx == _size )); then
echo $(( _cjmp + 1 ))
return
fi
if (( _idx > _size )) || (( ${_steps[_idx]:-0} )) || (( _arr[_idx] == 0 )); then
continue
fi
if (( _arr[_idx] == 1 )); then
_steps[_idx]=1
_q_idx+=("$_idx")
_q_jmp+=($(( _cjmp + 1 )))
fi
done
done
echo -1
}
This precomputes Fibonacci jumps, then uses a breadth-first search to find the shortest valid path across the river.