Advertisement
jkavart

Untitled

Feb 12th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.80 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5. void hanoiAux1ToAux3(int&, string, string, string, int);
  6. //builds Hanoi at A3 or A1 depending on the order you input paramaters
  7. void hanoiAux1toDest(int&,string, string, string, string, string, int);
  8. //Takes Hanoi from A1->Dest, played after largest rung reaches Dest
  9. void hanoiMain(int&,string, string,string,string,string, int, bool);
  10. //main function which recursively calls the rest
  11.  
  12. void hanoiAux1ToAux3(int &count,  string aux1, string aux2, string aux3,  int n)
  13. {
  14.     if(n==1)
  15.     {
  16.         count++; cout<<n<<" "<<aux1<<"-->"<<aux2<<" "<<count<<endl;
  17.         count++; cout<<n<<" "<<aux2<<"-->"<<aux3<<" "<<count<<endl;
  18.     }
  19.  
  20.     if(n>=2)
  21.     {
  22.         hanoiAux1ToAux3(count,aux1, aux2, aux3,n-1);
  23.         count++; cout<<n<<" "<<aux1<<"-->"<<aux2<<" "<<count<<endl;
  24.         hanoiAux1ToAux3(count,aux3, aux2, aux1,n-1);
  25.         count++; cout<<n<<" "<<aux2<<"-->"<<aux3<<" "<<count<<endl;
  26.         hanoiAux1ToAux3(count,aux1, aux2, aux3,n-1);
  27.     }
  28. }
  29. void hanoiMain(int &count, string start, string aux1, string aux2, string aux3, string dest, int n, bool biggestRung=true)
  30. {//boolean variable will be true if function is not called recursively, thus
  31.     if(n==1)//you are dealing with the biggest rung.
  32.     {
  33.         count++; cout<<n<<" "<<start<<"-->"<<aux1<<" "<<count<<endl;
  34.         hanoiAux1ToAux3(count,aux1, aux2, aux3, n);
  35.         if(biggestRung)
  36.         {
  37.             count++; cout<<n<<" "<<aux3<<"-->"<<dest<<" "<<count<<endl;
  38.         }
  39.     }
  40.     if(n>=2)
  41.     {
  42.         hanoiMain(count,start, aux1, aux2, aux3, dest, n-1, false);
  43.         count++; cout<<n<<" "<<start<<"-->"<<aux1<<" "<<count<<endl;
  44.         count++; cout<<n<<" "<<aux1<<"-->"<<aux2<<" "<<count<<endl;
  45.         hanoiAux1ToAux3(count,aux3, aux2, aux1,n-1);
  46.         count++; cout<<n<<" "<<aux2<<"-->"<<aux3<<" "<<count<<endl;
  47.         if(biggestRung)
  48.         {
  49.             count++; cout<<n<<" "<<aux3<<"-->"<<dest<<" "<<count<<endl;
  50.             hanoiAux1toDest(count,start, aux1, aux2, aux3, dest, n-1);
  51.         } else
  52.         {
  53.             hanoiAux1ToAux3(count,aux1, aux2, aux3,n-1);
  54.         }
  55.     }
  56. }
  57. void hanoiAux1toDest(int &count, string start, string aux1, string aux2, string aux3, string dest, int n)
  58. {
  59.     if(n==1)
  60.     {
  61.         hanoiAux1ToAux3(count,aux1, aux2, aux3, n);
  62.         count++; cout<<n<<" "<<aux3<<"-->"<<dest<<" "<<count<<endl;
  63.     }
  64.     if(n>=2)
  65.     {
  66.         hanoiAux1ToAux3(count,aux1, aux2, aux3,n-1);
  67.         count++; cout<<n<<" "<<aux1<<"-->"<<aux2<<" "<<count<<endl;
  68.         hanoiAux1ToAux3(count,aux3, aux2, aux1,n-1);
  69.         count++; cout<<n<<" "<<aux2<<"-->"<<aux3<<" "<<count<<endl;
  70.         count++; cout<<n<<" "<<aux3<<"-->"<<dest<<" "<<count<<endl;
  71.         hanoiAux1toDest(count,start, aux1, aux2, aux3, dest, n-1);
  72.     }
  73. }
  74.  
  75. int main()
  76. {
  77.    
  78.    
  79.     int rungs=1;
  80.         int counter=0;
  81.         for(rungs; rungs<11; rungs++)
  82.         {
  83.             counter=0;
  84.             cout<<rungs<<" RUNGS"<<endl;
  85.             hanoiMain(counter, "Strt", "Aux1", "Aux2", "Aux3", "Dest", rungs);
  86.             cout<<endl;
  87.         }
  88.  
  89.     system("pause");
  90.     return 0;
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement