Advertisement
makispaiktis

The 8 Queens Problem

Apr 14th, 2019 (edited)
304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.49 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4.  
  5. using namespace std;
  6.  
  7. // Helpful functions
  8. void displayStars(int n){
  9.     for(int i=0; i<n; i++){
  10.         cout << "*";
  11.     }
  12.     cout << endl;
  13. }
  14.  
  15. void displayVector(vector <int> v){
  16.     for(unsigned int i=0; i<v.size(); i++){
  17.         cout << v[i] << endl;
  18.     }
  19.     cout << endl;
  20. }
  21.  
  22. bool checkRowsOrColumns(vector <int> v){
  23.     bool flag = true;
  24.     for(unsigned int i=0; i<v.size()-1; i++){
  25.         for(unsigned int j=i+1; j<v.size(); j++){
  26.             if(v[i] == v[j]){
  27.                 flag = false;
  28.                 break;
  29.             }
  30.         }
  31.     }
  32.     return flag;                                            // If flag == true, no problem, but if flag == false, that means that there are i,j: v[i] = v[j], something we don't want for the solution
  33. }
  34.  
  35. bool checkDiagoniaDexia(vector <int> vx, vector <int> vy){
  36.     bool flag = true;
  37.     for(unsigned int i=0; i<vx.size(); i++){
  38.         for(unsigned int j=0; j<vy.size(); j++){
  39.             if (i == j){                                    // If i == j, we are comparing the same thing (example: when i=j=4, we are comparing the 5th pair with the 5th pair)
  40.                 continue;
  41.             }
  42.             else{
  43.                 if(vx[i] - vy[i] == vx[j] - vy[j]){
  44.                     flag = false;
  45.                     break;
  46.                 }
  47.             }
  48.         }
  49.     }
  50.     return flag;
  51. }
  52.  
  53. bool checkDiagoniaAristera(vector <int> vx, vector <int> vy){
  54.     bool flag = true;
  55.     for(unsigned int i=0; i<vx.size(); i++){
  56.         for(unsigned int j=0; j<vy.size(); j++){
  57.             if(i == j){
  58.                 continue;
  59.             }
  60.  
  61.             else{
  62.                 if(vx[i] + vy[i] == vx[j] + vy[j]){
  63.                     flag = false;
  64.                     break;
  65.                 }
  66.             }
  67.         }
  68.     }
  69.     return flag;
  70. }
  71.  
  72. int main()
  73. {
  74.     // 1. Intro
  75.     displayStars(100);
  76.     cout << "This is the 8 Queens Problem!" << endl;
  77.     cout << "You have to give me 8 positions of the 8 queens to tell you if you have found a solution." << endl;
  78.     cout << "Each position on the chess can be described with 2 numbers\nExample: If you want position 'e7' you have to write 5 and then 7\n\n";
  79.  
  80.     // 2. Create 2 vectors
  81.     vector <int> vx = vector <int> ();
  82.     vector <int> vy = vector <int> ();
  83.     for(int i=0; i<8; i++){
  84.         cout << "Position " << i+1 << ": ";
  85.         int x,y;
  86.         cin >> x;
  87.         vx.push_back(x);
  88.         cout << "            ";
  89.         cin >> y;
  90.         vy.push_back(y);
  91.         cout << endl;
  92.     }
  93.  
  94.     // 3. Display the tableau
  95.     char tableau[vx.size()][vy.size()];                                         // vx.size = vy.size = 8
  96.     for(int i=0; i<vx.size(); i++){
  97.         for(int j=0; j<vy.size(); j++){
  98.             tableau[i][j] = '-';
  99.         }
  100.     }
  101.  
  102.     for(int i=0; i<8; i++){
  103.         tableau[vx[i]-1][vy[i]-1] = 'Q';
  104.     }
  105.  
  106.     for(int i=0; i<vx.size(); i++){
  107.         for(int j=0; j<vy.size(); j++){
  108.             cout << tableau[i][j] << " ";
  109.         }
  110.         cout << endl;
  111.     }
  112.  
  113.     // 4. Check if this combination of positions is solution
  114.     if(checkRowsOrColumns(vx) == true && checkRowsOrColumns(vy) == true && checkDiagoniaDexia(vx, vy) == true &&checkDiagoniaAristera(vx, vy) == true){
  115.         cout << "Congratulations. This combination of chess choices is solution!\n\n";
  116.     }
  117.     else{
  118.         cout << "Unlucky! This is not a solution of this problem\n\n";
  119.     }
  120.  
  121.     return 0;
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement