Advertisement
jkavart

Untitled

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