A robot can make steps of three different lengths : 1 cm, 2 cm, 3cm. Write a Recursive Algorithm to find the number of different ways the robot can traverse the distance "d"
Asked
Active
Viewed 37 times
-3
-
2What have you tried? You can improve this question by demonstrating research effort and clarifying it into a specific question, rather than simply asking for a solution. See [How to ask](https://stackoverflow.com/help/how-to-ask) – Aposhian Feb 19 '21 at 05:16
-
Does this answer your question? [Recursive change-making algorithm](https://stackoverflow.com/questions/12520263/recursive-change-making-algorithm) – Aposhian Feb 19 '21 at 05:21
1 Answers
0
This will be the recursive algorithm
int findNumberOfWaysToTraverse(int d)
{
if (d == 1 || d == 0) // Base case: If the value of d is less than 0 then return 0, and if the value of d is equal to zero then return 1 as it is the starting place.
return 1;
else if (d == 2)
return 2;
else
return findNumberOfWaysToTraverse(d - 3) + //recursive calls
findNumberOfWaysToTraverse(d - 2) +
findNumberOfWaysToTraverse(d - 1);
}
Avishek Bhattacharya
- 5,717
- 3
- 30
- 46