Minimum Steps to reach a destination (Recursive method )
http://www.geeksforgeeks.org/minimum-steps-to-reach-a-destination/
GeeksforGeeks.
It is a ""branch and bound"" type of recursive problem, in which we have two options of going either left or Right in a number line.
And the target is to reach a particular point.
Starting Point is Zero.
#include<bits/stdc++.h>
using namespace std;
int fi(int dest,int steps,int cu)
{
if(cu==dest)
return steps;
if(abs(cu)>dest)
return INT_MAX;
return min(fi(dest,steps+1,steps+cu+1),fi(dest,steps+1,cu-steps-1));
}
int main()
{
int dest,i,j;
cin>>dest;
cout<<fi(dest,0,0)<<endl;
return 0;
}
GeeksforGeeks.
It is a ""branch and bound"" type of recursive problem, in which we have two options of going either left or Right in a number line.
And the target is to reach a particular point.
Starting Point is Zero.
#include<bits/stdc++.h>
using namespace std;
int fi(int dest,int steps,int cu)
{
if(cu==dest)
return steps;
if(abs(cu)>dest)
return INT_MAX;
return min(fi(dest,steps+1,steps+cu+1),fi(dest,steps+1,cu-steps-1));
}
int main()
{
int dest,i,j;
cin>>dest;
cout<<fi(dest,0,0)<<endl;
return 0;
}
Comments
Post a Comment