A backgammon player will be playing three consecutive matche
A backgammon player will be playing three consecutive matches with friends tonight. For each match, he will have the opportunity to place an even bet that he will win; the amount bet can be any quantity of his choice between zero and the amount of money he still has left after the bets on the preceding matches. For each match, the probability is 1 2 that he will win the match and thus win the amount bet, whereas the probability is 1 2 that he will lose the match and thus lose the amount bet. He will begin with S75, and his goal is to have $100 at the end. (Because these are friendly matches, he does not want to end up with more than $ 100.) Therefore, he wants to find the optimal betting policy (including all ties) that maximizes the probability that he will have exactly SI00 after the three matches. Use dynamic programming to solve this problem.
Solution
we will initialize the elements to -1
Here -1 means, so far not solved..
int dem[n+1];
int getMinSteps ( int n )
{
// parent case
if ( n == 1 ) return 0;
// already we done
if( dem[n] != -1 ) return dem[n];
int r = 1 + getMinSteps( n - 1 ); // \'-1\' step . \'r\' will contain the partial answer atlast
if( n%2 == 0 ) r = min( r , 1 + getMinSteps( n / 2 ) ) ; // \'/2\' step
if( n%3 == 0 ) r = min( r , 1 + getMinSteps( n / 3 ) ) ; // \'/3\' step
// save the result. If you forget to do this step, then recusrsion takes place again,
dem[n] = r ;
return r;
}
[/code]
