Hanoi towers by Aniol Garcia


/ Published in: C++
Save to your folder(s)

Hanoi tower solver


Copy this code and paste it in your HTML
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. void hanoi(int n, char origen, char desti, char aux)
  6. {
  7. if(n != 0)
  8. {
  9. hanoi(n-1,origen,aux,desti);
  10. cout << origen << " => " << desti << endl;
  11. hanoi(n-1,aux, desti, origen);
  12. }
  13. }
  14.  
  15. int main()
  16. {
  17. int n;
  18. cout << "Introdueix el nombre de discs: ";
  19. cin >> n;
  20. cout << "Els moviments que s'han de fer:\n";
  21. hanoi(n,'L','M','R'); // transfereix n discos de L a R utilitzant M
  22. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.