I'm attempting to write code for checking if you can reach the end of the array by following a certain rule. You start at the first element of an array of integers and the number stored in that position is how many hops you can make forward or backwards. The goal is to reach the end of the Vector which is represented by the 0 value.
bool Solvable(int start, Vector<int> & squares) {
int steps = squares[start];
int prev = start - steps;
int forward = start + steps;
if (prev >= 0) {
if (squares[prev] != squares[start]) {
return Solvable(prev, squares);
}
}
if (forward < squares.size()) {
if (squares[forward] == 0) return true;
if (squares[forward] != squares[start]) {
return Solvable(forward, squares);
}
}
return false;
}
The code does not seem to work because I think I am missing a base case, but I can't seem to figure out what other base case I need.
Thanks!