forked from VinayBelwal/Hactober-22-Programs-and-Projects-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
nQueen.cpp
54 lines (53 loc) · 1.04 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
#include <bits/stdc++.h>
using namespace std;
int check[100], col_series[100];
void print(int n)
{
c++;
for (int i = 0; i < n; i++)
cout << i + 1 << " " << col_series[i] + 1 << " ";
cout << endl
<< endl;
}
void nQueen(int row, int n)
{
for (int j = 0; j < n; j++)
{
if (check[j] != 1)
{
int r = row, d = 0;
bool cont = false;
while (r)
{
r--;
d++;
if (col_series[r] == j - d || col_series[r] == j + d)
{
cont = true;
break;
}
}
if (cont == true)
continue;
}
else
continue;
col_series[row] = j;
if (row == n - 1)
print(n);
else
{
check[j] = 1;
nQueen(row + 1, n);
check[j] = 0;
}
}
}
int main()
{
int n;
cout << "No. of queens: ";
cin >> n;
nQueen(0, n);
return 0;
}