Advertisement
jkavart

Untitled

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