Advertisement
jkavart

Untitled

Feb 8th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.57 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.  
  14.         Hanoi1(S, A1, A2, A3, D, n);
  15.     }
  16.    
  17.     if(n>=2)
  18.     {
  19.         Hanoi2(S, A1, A2, A3, D, n-1, false);
  20.         cout<<n<<" from "<<S<<" to "<<A1<<" to "<<A2<<endl;
  21.         Hanoi1(S, A3, A2, A1, D, n-1);
  22.         cout<<n<<" from "<<A2<<" to "<<A3<<endl;
  23.         if(firstTime)
  24.         {
  25.             cout<<n<<"from "<<A3<<" to "<<D<<endl;
  26.             Hanoi3(S, A1, A2, A3, D, n-1);
  27.         }
  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.  
  50. void Hanoi3(string S, string A1, string A2, string A3, string D, int n)
  51. {
  52.     if(n==1)
  53.     {
  54.         cout<<n<<"from "<<A1<<" to "<<A2<<" to "<<A3<<" to "<<D<<endl;
  55.     }
  56.     if(n>=2)
  57.     {
  58.         Hanoi1(S, A1, A2, A3, D, n-1);
  59.         cout<<n<<"from "<<A1<<" to "<<A2<<endl;
  60.         Hanoi1(S, A3, A2, A1, D, n-1);
  61.         cout<<n<<"from "<<A2<<" to "<<A3<<" to "<<D<<endl;
  62.         Hanoi3(S, A1, A2, A3, D, n-1);
  63.     }
  64. }
  65. int main()
  66. {
  67.     //test("s", "a", "d", 3);
  68.     Hanoi2("s", "a1", "a2", "a3", "d",
  69.         3);
  70.     system("pause");
  71.     return 0;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement