Minimum steps to reach a destination | C ++ program to count number of steps to reach a point ; source -> source vertex step -> value of last step taken dest -> destination vertex ; base cases ; if we go on positive side ; if we go on negative side ; minimum of both cases ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int steps ( int source , int step , int dest ) { if ( abs ( source ) > ( dest ) ) return INT_MAX ; if ( source == dest ) return step ; int pos = steps ( source + step + 1 , step + 1 , dest ) ; int neg = steps ( source - step - 1 , step + 1 , dest ) ; return min ( pos , neg ) ; } int main ( ) { int dest = 11 ; cout << " No . ▁ of ▁ steps ▁ required ▁ to ▁ reach ▁ " << dest << " ▁ is ▁ " << steps ( 0 , 0 , dest ) ; return 0 ; }