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