Advertisement
Taraxacum

Hanoi

Nov 16th, 2018
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.41 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. void move(char from, char to)
  6. {
  7.     cout << "Move from " << from << " to " << to << endl;
  8. }
  9.  
  10. void hanoi(char from, char to, char mid, int cnt)
  11. {
  12.     if (cnt == 1) {
  13.         move(from, to);
  14.     } else {
  15.         hanoi(from, mid, to, cnt - 1);
  16.         move(from, to);
  17.         hanoi(mid, to, from, cnt - 1);
  18.     }
  19. }
  20.  
  21. int main()
  22. {
  23.     hanoi('A', 'C', 'B', 3);
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement