daily pastebin goal
6%
SHARE
TWEET

Untitled

a guest Jan 17th, 2019 50 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <string>
  4. #include <stdio.h>
  5. #include <time.h>
  6. #include <fstream>
  7.  
  8. using namespace std;
  9.  
  10. int hanoi2(string, string, string, string, string, string, int, int&, int&);
  11. int hanoi1(string, string, string, string, string, string, int, int&, int&);
  12. void printMove(int, string, string);
  13.  
  14. int main()
  15. {
  16.     string Start = "Strt";  // Start
  17.     string Dest = "Dest";   // Destination
  18.     string A = "Aux1";      // Auxiliary 1
  19.     string B = "Aux2";      // Auxiliary 2
  20.     string C = "Aux3";      // Auxiliary 3
  21.     string D = "Aux4";      // Auxiliary 4
  22.     int numOfDisks;        
  23.     int movesMade = 0;     
  24.     int recursive = 0;     
  25.     int* m = &movesMade;    
  26.     int* r = &recursive;   
  27.    
  28.     clock_t start, finish;
  29.     cout << "Enter the number of Disks: ";
  30.     cin >> numOfDisks;
  31.  
  32.     while (numOfDisks > 0)
  33.     {
  34.         cout << endl;
  35.         cout << "Start" << endl << endl;
  36.    
  37.         //timing
  38.         start = clock();
  39.         hanoi1(Start, Dest, A, B, C, D, numOfDisks, *m, *r);
  40.         finish = clock();
  41.        
  42.         cout << endl << endl;
  43.         cout << "Total time taken to finsih: " << ((double)(finish-start)/CLOCKS_PER_SEC)*1000 <<   " milliseconds." << endl;
  44.         cout << "Total moves made: " << movesMade << endl;
  45.         cout << "Total number of recursive calls: " << recursive << endl << endl;
  46.            
  47.         movesMade = 0;
  48.         recursive = 0;
  49.      
  50.         cout << "Number of disks: ";
  51.         cin >> numOfDisks;
  52.     }
  53.    
  54.     cout << endl;
  55.     system("PAUSE");
  56.     return 0;
  57. }
  58. /*
  59.  * hanoi1 moves 1 disk
  60.  */
  61. int hanoi1(string Start, string Dest, string A, string B, string C, string D, int numOfDisks, int &ptr, int &ptr2)
  62. {
  63.     if (numOfDisks == 1)
  64.     {
  65.         printMove(numOfDisks, Start, A);
  66.         ptr++;
  67.         printMove(numOfDisks, A, B);
  68.         ptr++;
  69.         printMove(numOfDisks, B, C);
  70.         ptr++;
  71.         printMove(numOfDisks, C, D);
  72.         ptr++;
  73.         printMove(numOfDisks, D, Dest);
  74.         ptr++;
  75.         printMove(numOfDisks, Dest, Start);
  76.         return ptr;
  77.     }
  78.     else if (numOfDisks > 1)
  79.     {
  80.         hanoi2(Start, A, B, C, D, Dest, numOfDisks-1, ptr, ptr2);
  81.         printMove(numOfDisks, Start, Dest);
  82.         ptr++;
  83.         hanoi1(Dest, Start, A, B, C, D, numOfDisks-1, ptr, ptr2);
  84.         ptr2++;
  85.         hanoi1(Start, A, B, C, D, Dest, numOfDisks-1, ptr, ptr2);
  86.         ptr2++;
  87.    
  88.     }
  89.     return 0;
  90. };
  91. int hanoi2(string Start, string Dest, string A, string B, string C, string D, int numOfDisks, int &ptr, int &ptr2)
  92. {
  93.     if (numOfDisks == 1)
  94.     {
  95.         printMove(numOfDisks, Start, A);
  96.         ptr++;
  97.         printMove(numOfDisks, A, B);
  98.         ptr++;
  99.         printMove(numOfDisks, B, C);
  100.         ptr++;
  101.         printMove(numOfDisks, C, D);
  102.         ptr++;
  103.         printMove(numOfDisks, D, Dest);
  104.         ptr++;
  105.         printMove(numOfDisks, Dest, Start);
  106.         return ptr;
  107.     }
  108.     else if (numOfDisks > 1)
  109.     {
  110.         hanoi2(Start, A, B, C, D, Dest, numOfDisks-1, ptr, ptr2);    // recursive call with numOfDisks-1 disks
  111.         ptr2++;
  112.         printMove(numOfDisks, Start, A);
  113.         ptr++;
  114.         printMove(numOfDisks, A, B);
  115.         ptr++;
  116.         printMove(numOfDisks, B, C);
  117.         ptr++;
  118.         printMove(numOfDisks, C, D);
  119.         ptr++;
  120.         hanoi1(Dest, Start, A, B, C, D, numOfDisks-1, ptr, ptr2);
  121.         printMove(numOfDisks, D, Dest);
  122.         ptr++;
  123.         hanoi2(Start, A, B, C, D, Dest, numOfDisks-1, ptr, ptr2);
  124.         ptr2++;
  125.     }
  126.     return 0;
  127.    
  128. };
  129. void printMove(int numOfDisks, string from, string to)
  130. {
  131.     cout << endl << "Move disk (" << numOfDisks << ") from (" << from << ") to (" << to << ")\t";
  132.  
  133. };
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top