Advertisement
coloriot

HA12_Hanoy

Jun 26th, 2024
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.64 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. // Ханойские башни
  4.  
  5. int free_plate(int from, int to){
  6.     return 6 - from - to;
  7. }
  8.  
  9. void Hanoi(int size, int from, int to) {
  10.     if (size == 1) {
  11.         std:: cout << "Move disk 1 from plate " << from << " to plate " << to << std:: endl;
  12.         return;
  13.     }
  14.     int free = free_plate(from, to);
  15.     Hanoi(size - 1, from, free);
  16.     std:: cout << "Move disk " << size << " from plate " << from << " to plate " << to << std:: endl;
  17.     Hanoi(size - 1, free, to);
  18. }
  19.  
  20. int main() {
  21.     int n;
  22.     std:: cout << "Enter the number of disks: ";
  23.     std:: cin >> n;
  24.     Hanoi(n, 1, 3);
  25.     return 0;
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement