Posted By

agarcia on 04/16/15


Tagged


Versions (?)

Hanoi towers by Aniol Garcia


 / Published in: C++
 

Hanoi tower solver

  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  

You need to login to post a comment.