Advertisement
Guest User

Untitled

a guest
Feb 20th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.49 KB | None | 0 0
  1. #include <iostream>
  2. #include <chrono>
  3.  
  4. using namespace std;
  5. using namespace std::chrono;
  6.  
  7. bool conflict(int row, int col, int row2, int col2);
  8. bool isSafe(int* board, int row, int col);
  9. void printBoard(int* board, int n);
  10. int iterativeQueens(int n, bool print);
  11. int recursiveQueens(int n, bool print);
  12. void recursiveQueens(int* board, int n, int col, int& solutions, bool print);
  13. int* createBoard(int n);
  14.  
  15.  
  16. int main()
  17. {
  18.     auto start = duration_cast< milliseconds >(system_clock::now().time_since_epoch());
  19.  
  20.     auto sol = recursiveQueens(12, false); // 12x12
  21.     auto finish = duration_cast< milliseconds >(system_clock::now().time_since_epoch()) - start;
  22.     cout << sol << " Solutions found." << endl;
  23.     cout << "Time = " << finish.count() << " ms." << endl;
  24.     system("pause");
  25.     return 0;
  26. }
  27.  
  28. int recursiveQueens(int n, bool print)
  29. {
  30.     auto solutions = 0;
  31.     auto board = createBoard(n);
  32.     recursiveQueens(board, n, 0, solutions, print);
  33.     return solutions;
  34. }
  35. void recursiveQueens(int* board, int n, int col, int& solutions, bool print)
  36. {
  37.     if(col == n)
  38.     {
  39.         if(print)
  40.             printBoard(board, n);
  41.         solutions++;
  42.         return;
  43.     }
  44.     for(auto r = 0; r < n; r++)
  45.     {
  46.         if(isSafe(board, r, col))
  47.         {
  48.             board[col] = r;
  49.             recursiveQueens(board, n, col + 1, solutions, print);
  50.         }
  51.     }
  52. }
  53. bool isSafe(int* board, int row, int col)
  54. {
  55.     for(auto i = 0; i < col;i++)
  56.     {
  57.         auto c = i;
  58.         auto r = board[i];
  59.         if (conflict(r, c, row, col))
  60.             return false;
  61.     }
  62.     return true;
  63. }
  64. int* createBoard(int n)
  65. {
  66.     auto board = static_cast<int*>(malloc(n * sizeof(int)));
  67.     for(auto i = 0; i < n;i++)
  68.     {
  69.         board[i] = -1;
  70.     }
  71.     return board;
  72. }
  73. int iterativeQueens(int n, bool print) {
  74.     auto board = createBoard(n);
  75.     auto solutions = 0;
  76.  
  77.     auto row = 0;
  78.     while (row >= 0) {
  79.         do {
  80.             board[row]++;
  81.         } while (board[row] < n && !isSafe(board, board[row], row));
  82.         if (board[row] < n) {
  83.             if (row < n - 1) {
  84.                 board[++row] = -1;
  85.             } else {
  86.                 if (print)
  87.                     printBoard(board, n);
  88.                 solutions++;
  89.             }
  90.         }
  91.         else {
  92.             row--;
  93.         }
  94.     }
  95.     free(board);
  96.     return solutions;
  97. }
  98. void printBoard(int* board, int n)
  99. {
  100.     cout << "---------size = " << n << "---------" << endl;
  101.     for(int i = 0; i < n; i++)
  102.     {
  103.         for(int j = 0; j < n; j++)
  104.         {
  105.             if (board[i] == j)
  106.                 cout << "Q";
  107.             else
  108.                 cout << "*";
  109.         }
  110.         cout << endl;
  111.     }
  112.     cout << "--------------------------" << endl;
  113. }
  114. bool conflict(int row, int col, int row2, int col2) {
  115.     return row == row2 || col == col2 || abs(col - col2) == abs(row - row2);
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement