-LIR-

TOH

Jun 16th, 2018
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.42 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <windows.h>
  4.  
  5. using namespace std;
  6.  
  7. int n;
  8. vector<int> A, B, C;
  9.  
  10. void Initiate()
  11. {
  12.     cin >> n;
  13.     for( int i=n ; i>=1 ; i-- )
  14.         A.push_back(i);
  15.  
  16.     system("cls");
  17. }
  18.  
  19. void Show()
  20. {
  21.     system("cls");
  22.  
  23.     cout << endl << "A: ";
  24.     for( int i=0 ; i<A.size() ; i++ )
  25.         cout << A[i] << " ";
  26.  
  27.     cout << endl << "B: ";
  28.     for( int i=0 ; i<B.size() ; i++ )
  29.         cout << B[i] << " ";
  30.  
  31.     cout << endl << "C: ";
  32.     for( int i=0 ; i<C.size() ; i++ )
  33.         cout << C[i] << " ";
  34.  
  35.     cout << endl;
  36. }
  37.  
  38. void Move( int disk )
  39. {
  40.     int location = 1;
  41.     for( int i=1 ; i<=3 ; i++ )
  42.     {
  43.         if( i == 1 && A[A.size()-1] == disk )
  44.         {
  45.             location = 1;
  46.             break;
  47.         }
  48.         else if( i == 2 && B[B.size()-1] == disk )
  49.         {
  50.             location = 2;
  51.             break;
  52.         }
  53.         else if( i == 3 && C[C.size()-1] == disk )
  54.         {
  55.             location = 3;
  56.             break;
  57.         }
  58.     }
  59.  
  60.     int nextLocation = location;
  61.  
  62.     for( int i=1 ; i<=2 ; i++ )
  63.     {
  64.         nextLocation ++;
  65.  
  66.         if( nextLocation > 3 )
  67.             nextLocation = nextLocation%3;
  68.  
  69.         if( nextLocation == 1 )
  70.             if( A.empty() )
  71.                 i=3;
  72.             else if( A[A.size()-1] > disk )
  73.                 i=3;
  74.  
  75.         if( nextLocation == 2 )
  76.             if( B.empty() )
  77.                 i=3;
  78.             else if( B[B.size()-1] > disk )
  79.                 i=3;
  80.  
  81.         if( nextLocation == 3 )
  82.             if( C.empty() )
  83.                 i=3;
  84.             else if( C[C.size()-1] > disk )
  85.                 i=3;
  86.     }
  87.  
  88.     if( location == 1 )
  89.         A.pop_back();
  90.     else if( location == 2 )
  91.         B.pop_back();
  92.     else if( location == 3 )
  93.         C.pop_back();
  94.  
  95.     if( nextLocation == 1 )
  96.         A.push_back(disk);
  97.     else if( nextLocation == 2 )
  98.         B.push_back(disk);
  99.     else if( nextLocation == 3 )
  100.         C.push_back(disk);
  101.  
  102. }
  103.  
  104. void SolveTOH_Recursively( int n_disks )
  105. {
  106.     if( n_disks == 1 )
  107.     {
  108.         Move(1);
  109.         Show();
  110.         Sleep(500);
  111.     }
  112.     else
  113.     {
  114.         SolveTOH_Recursively( n_disks-1 );
  115.         Move( n_disks );
  116.         Show();
  117.         Sleep(500);
  118.         SolveTOH_Recursively( n_disks-1 );
  119.     }
  120. }
  121.  
  122. int main()
  123. {
  124.     Initiate();
  125.     Show();
  126.     Sleep(500);
  127.     SolveTOH_Recursively(n);
  128.  
  129.     return 0;
  130. }
Advertisement
Add Comment
Please, Sign In to add comment