Computer Science, asked by babusambit6568, 1 year ago

How tower of hanoi is an example for recursion?

Answers

Answered by Snowden1738
0
Suppose there are three towers.
You've got N discs to move from A to C.
So, first of all, you need to move the top N-1 discs to B and move N to C. Then you need to put the N-1 discs again to C, and then your goal is complete. So the rough algorithm looks like:
Say S, A, D denote the source, auxillary and destination towers respectively, then:

solve(N, A, B, C)
{
solve(N-1, A, C, B);
PUSH N FROM A TO C
solve(N-1, B, A, C);
}
[Not talking about the base case here]

So, you see. That's recursion. :D
Similar questions