fork download
  1. #include <iostream>
  2. #include <string.h>
  3. using namespace std;
  4.  
  5. void TOH(int n , string src , string helper , string dest){
  6. if(n == 1){
  7. cout << "transfer disks " << n << " from " << src << " to " << dest << endl;
  8. return;
  9. }
  10.  
  11. //n-1 disks transter from src to helper using dest as helper
  12. TOH(n-1 , src , dest , helper);
  13.  
  14. cout << "transfer disks " << n << " from " << src << " to " << dest << endl;
  15. // n-1 disks transfer from helper to dest using src as helper
  16. TOH(n-1 , helper , src , dest);
  17. }
  18.  
  19. int main() {
  20. int n = 3;
  21. TOH(n , "A" , "B" , "C");
  22. // your code goes here
  23. return 0;
  24. }
Success #stdin #stdout 0s 5312KB
stdin
Standard input is empty
stdout
transfer disks 1 from A to C
transfer disks 2 from A to B
transfer disks 1 from C to B
transfer disks 3 from A to C
transfer disks 1 from B to A
transfer disks 2 from B to C
transfer disks 1 from A to C