There is nothing wrong with the function prototype (except that maybe you should pass the vector by reference instead of by value). The problem is with the code inside of it. I'm not sure how I can help you fix it without just giving you the code, but I'll try.
1 2 3 4 5 6 7 8 9 10 11 12 13
|
int cost (vector<int> gameBoard, int index)
{
if (index > 1)
{
int costOne = cost(gameBoard,(index-1));
int costTwo = cost(gameBoard,(index-2));
return min(costOne,costTwo) + gameBoard[index];
}
else if (index == 0 || index == 1)
return gameBoard[index];
}
|
So your function stops recursion when
index == 0 || index == 1
. But what about the rules we just talked about? The player starts from the beginning of the list. That means
index == 0
in the first call of the function. What does the code you wrote mean in light of that? It means stopping the "game" before we've even begun.
Instead of using that as a condition, use the condition for the end of the game... which is when the player reaches the end of the list, right? Think about how that would be in code.
Now in light of all that, what do you think should change about these lines?
1 2 3 4
|
if (index > 1)
{
int costOne = cost(gameBoard,(index-1));
int costTwo = cost(gameBoard,(index-2));
|
You also need to think about what you actually return from the function.
^Those are actually just intermediate steps, because the whole way you're going about this is wrong.
You're only looking at 2 numbers at a time. But what if the numbers you're looking at are
or even
Looking at only 2 numbers in those cases will not give you the most cost-efficient route.