Topics

Classic Tower of hanoi with 3 pegs: move a stack of n disks from one peg (source) to another peg (destination), using a third peg as an auxiliary, and following the rules:

  • Only one disk can be moved at a time
  • A larger disk cannot be placed on top of a smaller disk

// Problem: https://cses.fi/problemset/task/2165/
 
void move(int diskNum, int source, int destination) { 
    cout << source << " " << destination << "\n"; 
}
 
void towerOfHanoi(int n, int source, int destination, int helper) {
  if (n == 0)
    return;
 
  towerOfHanoi(n - 1, source, helper, destination);
  move(n, source, destination);
  towerOfHanoi(n - 1, helper, destination, source);
}