Advertisement
Lauda

Untitled

Mar 14th, 2013
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.29 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int NoS = 0;
  6.  
  7. bool isAttacking(int *board, int k)
  8. {
  9.     for (int i=0; i<k; i++)
  10.         if ((board[i] == board[k]) || (abs(board[i] - board[k])) == (k-i))
  11.             return false;
  12.     return true;
  13. }
  14.  
  15. void setQueen(int *board, int size, int k = 0)
  16. {
  17.     if (k != size)
  18.     {
  19.         for (int i=0; i<size; i++)
  20.         {
  21.             board[k] = i;
  22.             if (isAttacking(board, k))
  23.                 setQueen(board, size, k+1);
  24.         }
  25.     }
  26.     else
  27.         NoS++;
  28. }
  29.  
  30. int main(int argc, char *argv[])
  31. {
  32.     int input = 0; // Chessboard input dim.
  33.    
  34.     // We check args now, k?
  35.     if ((argc < 2) || (argc > 2))
  36.     {
  37.         do
  38.         {
  39.             cout << "Please input chessboard dim.: " << endl;
  40.             cin >> input;
  41.            
  42.             if ((input < 1) || (input > 15))
  43.                 cout << "Suggessted input value is between 1 and 15!" << endl;
  44.         } while ((input < 1) || (input > 15));
  45.     }
  46.     else
  47.         input = atoi(argv[1]); // We take arg value...no check for value, yet
  48.        
  49.         cout << "Chessboard dim. is: " << input << "x" << input << endl;
  50.        
  51.         int *board = new int[input];
  52.         for (int i=0; i<input; i++)
  53.             board[i] = 0; // Initialize this mfer
  54.            
  55.         setQueen(board, input);
  56.         cout << "Count of all possible solutions is: " << NoS << endl;
  57.        
  58.         delete[] board; // mem leak
  59.         NoS = 0;
  60.        
  61.         system("pause");
  62.         return 0;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement