π One-stop destination for all your technical interview Preparation π
Tower of Hanoi is a mathematical puzzle where we have three rods and n disks. The objective of the puzzle is to move the entire stack to another rod, obeying the following simple rules:
void towerOfHanoi(int n, char source, char dest, char temp)
{
if (n == 1)
{
cout << "Move disk 1 from " << source << " to " << dest << endl;
return;
}
towerOfHanoi(n - 1, source, temp, dest);
cout << "Move disk " << n << " from " << source << " to " << dest << endl;
towerOfHanoi(n - 1, temp, dest, source);
return;
}