-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnQueens.cpp
More file actions
50 lines (39 loc) · 1.19 KB
/
nQueens.cpp
File metadata and controls
50 lines (39 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//============================================================================
// Name : nQueens.cpp
// Author : Imogen Cleaver-Stigum & Jyalu Wu
// Version :
// Copyright : 2019 IMOLU
// Description :
//============================================================================
#include <iostream>
using namespace std;
// this is not a header file
int isLegalPosition (int* board, int n);
int main();
int main() {
int n = 8;
int board[8] = {1, 0, -1, -1, -1, -1, -1, -1};
return 0;
}
int isLegalPosition (int* board, int n) {
int isLegal = 1;
// assume there are no same-row queens since the queens are places row-by-row
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) { // check for same-column queens
if (board[i] == board[j] && board[i] != -1 && i != j) {
isLegal = 0;
}
}
if (i < n-1) { // check for same-diagonal queens on negative-slope diagonals
if (board[i] == board[i+1]-1 && board[i]!= -1 && board[i+1] != -1) {
isLegal = 0;
}
}
if (i > 0) { // check for same-diagonal queens on positive-slope diagonals
if (board[i] == board[i-1]-1 && board[i]!= -1 && board[i-1] != -1) {
isLegal = 0;
}
}
}
return isLegal;
}