Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iomanip>
- #include <iostream>
- #include <string>
- using namespace std;
- void Hanoi1(string S, string A1, string A2, string A3, string D, int n, bool firstTime=true, bool nextTime=false, bool largest=false)
- {
- if(firstTime)
- {
- nextTime=true;
- largest=true;
- }
- if(firstTime)
- {
- cout<<1<<" from "<<S<<" to "<<A1<<endl;
- }
- if(n==1)
- {
- cout<<n<<" from "<<A1<<" to "<<A2<<" to "<<A3<<endl;
- if(largest)
- cout<<n<<" from "<<A3<<" to "<<D<<endl;
- }
- if(n>=2)
- {
- Hanoi1(S, A1, A2, A3, D, n-1, false, true);
- if(n>=2&&nextTime)
- cout<<n<<"from "<<S<<" to "<<A1<<endl;
- cout<<n<<" from "<<A1<<" to "<<A2<<endl;
- Hanoi1(S, A3, A2, A1, D, n-1, false);
- cout<<n<<" from "<<A2<<" to "<<A3<<endl;
- if(largest)
- {
- cout<<n<<" from "<<A3<<" to "<<D<<endl;
- }
- Hanoi1(S, A1, A2, A3, D, n-1, false,false,true);
- }
- }
- int main()
- {
- //test("s", "a", "d", 3);
- Hanoi1("s", "a1", "a2", "a3", "d", 3);
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement