-
Notifications
You must be signed in to change notification settings - Fork 7
/
N_queen_problem.cpp
95 lines (85 loc) · 2.17 KB
/
N_queen_problem.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <string>
#include <queue>
#include <vector>
#include <set>
#include <map>
using namespace std;
typedef long long int lint;
int totalcalls = 0;
int c = 0;
bool isSafe(vector<vector <int> > &board, int i, int j, int N) {
for (int row = 0; row < i; ++row) {
if (board[row][j] == 1) {
return false;
}
}
// check for left upper diagonal
int x = i, y = j;
while (x >= 0 && y >= 0) {
if (board[x][y] == 1) {
return false;
}
--x;
--y;
}
// check for right upper diagonal
x = i, y = j;
while (x >= 0 && y < N) {
if (board[x][y] == 1) {
return false;
}
--x;
++y;
}
// so, position is safe for queen.
return true;
}
void printBoard(vector<vector <int> > &board, int N) {
c++;
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
if (board[i][j] == 1) {
cout << "Q";
} else {
cout << "_ ";
}
}
cout << endl;
}
}
bool solveNqueen(vector<vector <int> > &board, int i, int N) {
totalcalls++;
// Base case
if (i == N) {
// print the board.
printBoard(board, N);
cout << endl << endl;
return false; // for one sol.return true else return false for printing all the possible solutions.
}
for (int j = 0; j < N; ++j) {
if (isSafe(board, i, j, N)) {
board[i][j] = 1; // place the queen assuming i-j th is right position.
bool nextQueen = solveNqueen(board, i + 1, N);
if (nextQueen) {
return true;
}
// if i-j th position is not right - assumption is wrong
board[i][j] = 0; // backtrack.. unchoose
}
}
// couldn't place queen.
return false;
}
int main() {
int N;
cin >> N;
vector<vector <int> >board(N, vector<int>(N, 0));
solveNqueen(board, 0, N);
cout << "totalcalls : " << totalcalls << endl;
cout << "total solutions: " << c << endl;
return 0;
}