Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int NoS = 0;
- bool isAttacking(int *board, int k)
- {
- for (int i=0; i<k; i++)
- if ((board[i] == board[k]) || (abs(board[i] - board[k])) == (k-i))
- return false;
- return true;
- }
- void setQueen(int *board, int size, int k = 0)
- {
- if (k != size)
- {
- for (int i=0; i<size; i++)
- {
- board[k] = i;
- if (isAttacking(board, k))
- setQueen(board, size, k+1);
- }
- }
- else
- NoS++;
- }
- int main(int argc, char *argv[])
- {
- int input = 0; // Chessboard input dim.
- // We check args now, k?
- if ((argc < 2) || (argc > 2))
- {
- do
- {
- cout << "Please input chessboard dim.: " << endl;
- cin >> input;
- if ((input < 1) || (input > 15))
- cout << "Suggessted input value is between 1 and 15!" << endl;
- } while ((input < 1) || (input > 15));
- }
- else
- input = atoi(argv[1]); // We take arg value...no check for value, yet
- cout << "Chessboard dim. is: " << input << "x" << input << endl;
- int *board = new int[input];
- for (int i=0; i<input; i++)
- board[i] = 0; // Initialize this mfer
- setQueen(board, input);
- cout << "Count of all possible solutions is: " << NoS << endl;
- delete[] board; // mem leak
- NoS = 0;
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement