I would do something like this where you keep track of what current index you are currently on and the sum so far up to the point of that index following the path (so the sum would exclude the numbers we skipped).
public static int play(int[] a, int index, int sum){
if (index>=a.length) return Integer.MAX_VALUE; //Path went over last index. Invalid Path.
sum += a[index]; //Add on to the sum of the index you are currently on
if (index == a.length-1){
return sum; //If we are at last element, return sum of the path traveled
}else{
return Math.min(play(a, index+1, sum), play(a, index+2, sum)); //Continue onto path checking both options
}
}
Test Code:
public static void main(String args[]) {
int[] a = new int[]{0, 4, 23, 566, 34, 45, 555, 11, 34, 35, 45, 65, 55, 98, 344};
System.out.println(play(a, 0, 0));
}
Output: 591
Hope this helps.
Edit:
Okay, removing all the code and just thinking about this logically. The way I like to think of recursion (especially with paths) is that I am sending out individual little men and after each recursion I am going to duplicate those little men to take different paths. Lets use the smaller array, [0,1,3,4,1], for this example.
We start off with one little man. He starts off at 0. He has 2 options. First option is to go to 1 and second option is to go to 3. We need to go down both of these paths since we are unsure what lies ahead. The little man duplicates himself sending one person to 1 and another to 3.
Now the little man that went to path 1 has 2 more options laying ahead. He can either go to 3 or 4. The little man that went to path 3 also has 2 more options. He can go to 4 or 1. So they duplicate themselves and send the little men through all the options. I'm hoping this all makes sense at this point. The code that handles the duplication of the little man to different paths is: play(a, index+1, sum), play(a, index+2, sum))
What does a little man do once it reaches the end of the path? He has to tell the original little man how far the path is. The code for this is: if (index == a.length-1){return sum;}
The original little man, though, doesn't care about that one path. He needs to compare it to the counterpart that went the other path to see which one is shorter. That is controlled by the Math.Min. Which just returns the shorter distance of the 2 little men. So the little man that has the shorter of the distances kills his clone (:p) since we do not like that path. Eventually all of the little men will kill each other leaving only one little man standing which is the shortest distance. :)