Advertisement
jkavart

Untitled

Feb 10th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.61 KB | None | 0 0
  1. #include <iomanip>
  2. #include <iostream>
  3. #include <string>
  4.  
  5. using namespace std;
  6. void Hanoi3(string S, string A1, string A2, string A3, string D, int n);
  7. void Hanoi1(string S, string A1, string A2, string A3, string D, int n);
  8. void Hanoi2(string S, string A1, string A2, string A3, string D, int n, bool firstTime=true)
  9. {
  10.     if(n==1)
  11.     {
  12.         cout<<n<<" from "<<S<<" to "<<A1<<endl;
  13.         Hanoi1(S, A1, A2, A3, D, n);
  14.     }
  15.     if(n>=2)
  16.     {
  17.         Hanoi2(S, A1, A2, A3, D, n-1, false);
  18.         cout<<n<<" from "<<S<<" to "<<A1<<" to "<<A2<<endl;
  19.         Hanoi1(S, A3, A2, A1, D, n-1);
  20.         cout<<n<<" from "<<A2<<" to "<<A3<<endl;
  21.         if(firstTime)
  22.         {
  23.             cout<<n<<"from "<<A3<<" to "<<D<<endl;
  24.             Hanoi3(S, A1, A2, A3, D, n-1);
  25.         } else
  26.         {
  27.             Hanoi1(S, A1, A2, A3, D, n-1);
  28.         }
  29.     }
  30. }
  31.  
  32. void Hanoi1(string S, string A1, string A2, string A3, string D, int n)
  33. {
  34.     if(n==1)
  35.     {
  36.         cout<<n<<" from "<<A1<<" to "<<A2<<" to "<<A3<<endl;
  37.     }
  38.  
  39.     if(n>=2)
  40.     {
  41.         Hanoi1(S, A1, A2, A3, D, n-1);
  42.         cout<<n<<" from "<<A1<<" to "<<A2<<endl;
  43.         Hanoi1(S, A3, A2, A1, D, n-1);
  44.         cout<<n<<" from "<<A2<<" to "<<A3<<endl;
  45.         Hanoi1(S, A1, A2, A3, D, n-1);
  46.     }
  47. }
  48.  
  49. void Hanoi3(string S, string A1, string A2, string A3, string D, int n)
  50. {
  51.     if(n==1)
  52.     {
  53.         cout<<n<<"from "<<A1<<" to "<<A2<<" to "<<A3<<" to "<<D<<endl;
  54.     }
  55.     if(n>=2)
  56.     {
  57.         Hanoi1(S, A1, A2, A3, D, n-1);
  58.         cout<<n<<"from "<<A1<<" to "<<A2<<endl;
  59.         Hanoi1(S, A3, A2, A1, D, n-1);
  60.         cout<<n<<"from "<<A2<<" to "<<A3<<" to "<<D<<endl;
  61.         Hanoi3(S, A1, A2, A3, D, n-1);
  62.     }
  63. }
  64. int main()
  65. {
  66.     //test("s", "a", "d", 3);
  67.     Hanoi2("s", "a1", "a2", "a3", "d",
  68.         3);
  69.     system("pause");
  70.     return 0;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement