-
Notifications
You must be signed in to change notification settings - Fork 0
/
NQueen.cpp
61 lines (55 loc) · 1.06 KB
/
NQueen.cpp
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
51
52
53
54
55
56
57
58
59
60
61
#include<iostream>
using namespace std;
bool isValidMove(int newRow, int newCol, int size, bool *board){
bool valid = true;
for(int i=0; i<size; i++){
if(board[i*size+newCol]){
valid = false;
}
}
for(int i=newRow,j=newCol; i>=0 && j>=0; i--,j--){
if(board[i*size+j]){
valid = false;
}
}
for(int i=newRow, j=newCol; i>=0 && j<size; i--,j++){
if(board[i*size+j]){
valid = false;
}
}
return valid;
}
bool nqueen(int row, int size, bool *board){
if(row == size-1){
for(int i=0; i<size; i++){
for(int j=0; j<size; j++){
cout<<board[i*size+j]<<" ";
}
cout<<endl;
}
return true;
}
else{
for(int col=0; col<size; col++){
int newRow = row + 1;
if(isValidMove(newRow,col,size,board)){
board[newRow*size+col] = true;
if(nqueen(newRow,size,board)){
return true;
}
board[newRow*size+col] = false;
}
}
return false;
}
}
int main() {
bool b[][4] = {
{false,false,false,false},
{false,false,false,false},
{false,false,false,false},
{false,false,false,false}
};
nqueen(-1,4,&b[0][0]);
return 0;
}