Code:
void move_rings(int n, int src, int dest, int other) {
const char *indent = " ";
cout << &indent[n] << "Call(" << n << "," << src << "," << dest << "," << other << ")" << endl;
if (n == 1) {
move_a_ring(src, dest);
} else {
move_rings(n - 1, src, other, dest);
move_a_ring(src, dest);
move_rings(n - 1, other, dest, src);
}
cout << &indent[n] << "Retn(" << n << "," << src << "," << dest << "," << other << ")" << endl;
}
$ g++ foo.cpp
$ ./a.out
Call(3,1,3,2)
Call(2,1,2,3)
Call(1,1,3,2)
Move from 1 to 3
Retn(1,1,3,2)
Move from 1 to 2
Call(1,3,2,1)
Move from 3 to 2
Retn(1,3,2,1)
Retn(2,1,2,3)
Move from 1 to 3
Call(2,2,3,1)
Call(1,2,1,3)
Move from 2 to 1
Retn(1,2,1,3)
Move from 2 to 3
Call(1,1,3,2)
Move from 1 to 3
Retn(1,1,3,2)
Retn(2,2,3,1)
Retn(3,1,3,2)