Advertisement
AbsolutelyS

Untitled

May 12th, 2024
716
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.49 KB | None | 0 0
  1. /*
  2.  * Towers of Hanoi
  3.  */
  4.  
  5. void move(int disk, char source, char destination) {
  6.     cout << "Move disk " << disk << " from rod " << source << " to rod " << destination << endl;
  7. }
  8.  
  9. void towersOfHanoi(int n, char source, char destination, char auxiliary) {
  10.     if (n == 1) {
  11.         move(n, source, destination);
  12.         return;
  13.     }
  14.  
  15.     towersOfHanoi(n - 1, source, auxiliary, destination);
  16.     move(n, source, destination);
  17.     towersOfHanoi(n - 1, auxiliary, destination, source);
  18. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement