Advertisement
Guest User

Untitled

a guest
Nov 12th, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.73 KB | None | 0 0
  1. void hanoi(vector<VI>& moves, int n_twrs, int n_dsks, VI& aux) {
  2.     int start = aux[0];
  3.     int goal = aux[1];
  4.        
  5.     if (n_dsks == 1)
  6.         moves.push_back(VI { aux[0], aux[1] });
  7.  
  8.     else {
  9.         int k = k_hanoi[n_twrs][n_dsks];
  10.        
  11.         // moves top k disks from start to sparing tower
  12.         int sparing = aux[n_twrs-1];
  13.         aux[1] = sparing;
  14.         aux[n_twrs-1] = goal;
  15.         hanoi(moves, n_twrs, k, aux);
  16.        
  17.         // moves bottom n-k disks from start to goal
  18.         aux[0] = start;
  19.         aux[1] = goal;
  20.         hanoi(moves, n_twrs - 1, n_dsks - k, aux);
  21.        
  22.         // moves original k disks from sparing tower to goal
  23.         aux[0] = sparing;
  24.         aux[n_twrs-1] = start;
  25.         hanoi(moves, n_twrs, k, aux);
  26.     }
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement