Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- using namespace std;
- void hanoiAux1ToAux3(int&, string, string, string, int);
- //builds Hanoi at A3 or A1 depending on the order you input paramaters
- void hanoiAux1toDest(int&,string, string, string, string, string, int);
- //Takes Hanoi from A1->Dest, played after largest rung reaches Dest
- void hanoiMain(int&,string, string,string,string,string, int, bool);
- //main function which recursively calls the rest
- void hanoiAux1ToAux3(int &count, string aux1, string aux2, string aux3, int n)
- {
- if(n==1)
- {
- count++; cout<<n<<" "<<aux1<<"-->"<<aux2<<" "<<count<<endl;
- count++; cout<<n<<" "<<aux2<<"-->"<<aux3<<" "<<count<<endl;
- }
- if(n>=2)
- {
- hanoiAux1ToAux3(count,aux1, aux2, aux3,n-1);
- count++; cout<<n<<" "<<aux1<<"-->"<<aux2<<" "<<count<<endl;
- hanoiAux1ToAux3(count,aux3, aux2, aux1,n-1);
- count++; cout<<n<<" "<<aux2<<"-->"<<aux3<<" "<<count<<endl;
- hanoiAux1ToAux3(count,aux1, aux2, aux3,n-1);
- }
- }
- void hanoiMain(int &count, string start, string aux1, string aux2, string aux3, string dest, int n, bool biggestRung=true)
- {//boolean variable will be true if function is not called recursively, thus
- if(n==1)//you are dealing with the biggest rung.
- {
- count++; cout<<n<<" "<<start<<"-->"<<aux1<<" "<<count<<endl;
- hanoiAux1ToAux3(count,aux1, aux2, aux3, n);
- if(biggestRung)
- {
- count++; cout<<n<<" "<<aux3<<"-->"<<dest<<" "<<count<<endl;
- }
- }
- if(n>=2)
- {
- hanoiMain(count,start, aux1, aux2, aux3, dest, n-1, false);
- count++; cout<<n<<" "<<start<<"-->"<<aux1<<" "<<count<<endl;
- count++; cout<<n<<" "<<aux1<<"-->"<<aux2<<" "<<count<<endl;
- hanoiAux1ToAux3(count,aux3, aux2, aux1,n-1);
- count++; cout<<n<<" "<<aux2<<"-->"<<aux3<<" "<<count<<endl;
- if(biggestRung)
- {
- count++; cout<<n<<" "<<aux3<<"-->"<<dest<<" "<<count<<endl;
- hanoiAux1toDest(count,start, aux1, aux2, aux3, dest, n-1);
- } else
- {
- hanoiAux1ToAux3(count,aux1, aux2, aux3,n-1);
- }
- }
- }
- void hanoiAux1toDest(int &count, string start, string aux1, string aux2, string aux3, string dest, int n)
- {
- if(n==1)
- {
- hanoiAux1ToAux3(count,aux1, aux2, aux3, n);
- count++; cout<<n<<" "<<aux3<<"-->"<<dest<<" "<<count<<endl;
- }
- if(n>=2)
- {
- hanoiAux1ToAux3(count,aux1, aux2, aux3,n-1);
- count++; cout<<n<<" "<<aux1<<"-->"<<aux2<<" "<<count<<endl;
- hanoiAux1ToAux3(count,aux3, aux2, aux1,n-1);
- count++; cout<<n<<" "<<aux2<<"-->"<<aux3<<" "<<count<<endl;
- count++; cout<<n<<" "<<aux3<<"-->"<<dest<<" "<<count<<endl;
- hanoiAux1toDest(count,start, aux1, aux2, aux3, dest, n-1);
- }
- }
- int main()
- {
- int rungs=1;
- int counter=0;
- for(rungs; rungs<11; rungs++)
- {
- counter=0;
- cout<<rungs<<" RUNGS"<<endl;
- hanoiMain(counter, "Strt", "Aux1", "Aux2", "Aux3", "Dest", rungs);
- cout<<endl;
- }
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement