Advertisement
Guest User

NxN queen

a guest
Jul 7th, 2014
389
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.38 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5. const int BSIZE = 8;
  6. char chessBoard[BSIZE][BSIZE];
  7.  
  8. struct qPos
  9. {
  10.     qPos() : h(0), v(0), found(true) {}
  11.     int h; //horizontal pos
  12.     int v; //vertical pos
  13.     bool found; //if position is available
  14. };
  15.  
  16. qPos findNextQPos(vector<qPos> Qs);
  17. void fillBoard(vector<qPos> Qs);
  18. void print();
  19. vector<qPos> generateDisallowed(vector<qPos> Qs);
  20. bool isDisallowed(qPos nextPos, vector<qPos> disallowedPos);
  21.  
  22. int main(int argc, char **argv[]){
  23.     vector<qPos> QsOnBoard; //Position of all the queens on board
  24.     qPos nextQ; //next possible position
  25.     while (nextQ.found)
  26.     {
  27.         nextQ = findNextQPos(QsOnBoard);
  28.         if (nextQ.found)
  29.         {
  30.             QsOnBoard.push_back(nextQ); //If the nextQ is available i.e. not disallowed, add it to the queens vector
  31.         }
  32.     }
  33.     fillBoard(QsOnBoard); //Fill the board with queens positions
  34.     print(); // print the board
  35.     return 0;
  36. }
  37.  
  38. qPos findNextQPos(vector<qPos> Qs) {
  39.     // Generate disallowed positions based on all the queens on board
  40.     vector <qPos> disallowedPos = generateDisallowed(Qs);
  41.     qPos nextQ;
  42.     for (size_t i = 0; i < BSIZE; i++)
  43.     {
  44.         for (size_t j = 0; j < BSIZE; j++)
  45.         {
  46.             nextQ.h = i;
  47.             nextQ.v = j;
  48.             if (!isDisallowed(nextQ, disallowedPos)) { //Check if next possible position is a disallowed position
  49.                 //cout << "Next available:\n" << nextQ.h << ", " << nextQ.v << endl;
  50.                 return nextQ; // if it is avaible return the position, break the loop
  51.             }
  52.         }
  53.     }
  54.     nextQ.found = false; // No available position is found to return, found is set to false, return the position
  55.     return nextQ;
  56. }
  57.  
  58. /**
  59. *   Generate a vector of positions which are not allowed based on
  60. *   a set of Queens on board
  61. **/
  62. vector<qPos> generateDisallowed(vector<qPos> Qs) {
  63.     vector <qPos> disallowedPos;
  64.     qPos tmpPos;
  65.     // Loop through all the queens positions on board
  66.     for (size_t i = 0; i < Qs.size(); i++)
  67.     {
  68.         // The queen positions is not allowed for next position
  69.         disallowedPos.push_back(Qs.at(i));
  70.         // All the positions with the same horizontal position are disallowed
  71.         tmpPos.h = Qs.at(i).h;
  72.         for (size_t k = 0; k < BSIZE; k++)
  73.         {
  74.             tmpPos.v = k;
  75.             disallowedPos.push_back(tmpPos);
  76.         }
  77.         // All the positions with the same vertical position are disallowed
  78.         tmpPos.v = Qs.at(i).v;
  79.         for (size_t k = 0; k < BSIZE; k++)
  80.         {
  81.             tmpPos.h = k;
  82.             disallowedPos.push_back(tmpPos);
  83.         }
  84.         for (size_t k = 0; k < Qs.at(i).h + Qs.at(i).v; k++)
  85.         {
  86.             tmpPos.h = 0 + k;
  87.             tmpPos.v = Qs.at(i).h + Qs.at(i).v - k;
  88.             disallowedPos.push_back(tmpPos);
  89.         }
  90.         // Diagonal positions are disallowed
  91.         //Sweep forwards, adding higher cells to diag1 and lower cells
  92.         for (int k = 1; Qs.at(i).h + k < BSIZE; k++) {
  93.             if (Qs.at(i).v - k >= 0) {
  94.                 tmpPos.h = Qs.at(i).h + k;
  95.                 tmpPos.v = Qs.at(i).v - k;
  96.                 disallowedPos.push_back(tmpPos);
  97.             }
  98.             if (Qs.at(i).v + k < BSIZE) {
  99.                 tmpPos.h = Qs.at(i).h + k;
  100.                 tmpPos.v = Qs.at(i).v + k;
  101.                 disallowedPos.push_back(tmpPos);
  102.             }
  103.         }
  104.         //Sweep backwards, adding higher cells to diag2 and lower cells to diag1
  105.         for (int k = 1; Qs.at(i).h - k >= 0; k++) {
  106.             if (Qs.at(i).v - k >= 0) {
  107.                 tmpPos.h = Qs.at(i).h - k;
  108.                 tmpPos.v = Qs.at(i).v - k;
  109.                 disallowedPos.push_back(tmpPos);
  110.             }
  111.             if (Qs.at(i).v + k < BSIZE) {
  112.                 tmpPos.h = Qs.at(i).h - k;
  113.                 tmpPos.v = Qs.at(i).v + k;
  114.                 disallowedPos.push_back(tmpPos);
  115.             }
  116.         }
  117.     }
  118.     return disallowedPos;
  119. }
  120.  
  121. /**
  122. *   check if the nextpos is disallowed
  123. **/
  124. bool isDisallowed(qPos nextPos, vector<qPos> disallowedPos) {
  125.     for (size_t i = 0; i < disallowedPos.size(); i++)
  126.     {
  127.         if (disallowedPos.at(i).h == nextPos.h &&
  128.             disallowedPos.at(i).v == nextPos.v)
  129.         {
  130.             return true;
  131.         }
  132.     }
  133.     return false;
  134. }
  135.  
  136. /**
  137. *   Fill the board with _ and Q for empty and queen positions
  138. **/
  139. void fillBoard(vector<qPos> Qs) {
  140.     for (size_t i = 0; i < BSIZE; i++)
  141.     {
  142.         for (size_t j = 0; j < BSIZE; j++)
  143.         {
  144.             chessBoard[i][j] = '_';
  145.         }
  146.     }
  147.     for (size_t i = 0; i < Qs.size(); i++)
  148.     {
  149.         chessBoard[Qs.at(i).v][Qs.at(i).h] = 'Q';
  150.     }
  151.     vector <qPos> disallowedPos = generateDisallowed(Qs);
  152. }
  153.  
  154. /**
  155. *   Print the board on the command line
  156. **/
  157. void print() {
  158.     cout << ' ';
  159.     for (size_t i = 0; i < BSIZE; i++)
  160.     {
  161.         cout << ' ' << i;
  162.     }
  163.     cout << endl;
  164.     for (size_t i = 0; i < BSIZE; i++)
  165.     {
  166.         cout << i << ' ';
  167.         for (size_t j = 0; j < BSIZE; j++)
  168.         {
  169.             cout << chessBoard[i][j] << ' ';
  170.         }
  171.         cout << '\n';
  172.     }
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement