-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnQueens.cpp
More file actions
77 lines (67 loc) · 1.4 KB
/
nQueens.cpp
File metadata and controls
77 lines (67 loc) · 1.4 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
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
#include <iostream>
using namespace std;
bool diag(int n, int r, int s, int sol[])
{
bool res = true;
for (int s1=0; s1<n; s1++)
{
if ((sol[s1]!=-1) &&(s1-s==sol[s1]-r))
{
res=false;
break;
}
if ((sol[s1]!=-1) && (s1-s==r-sol[s1]))
{
res=false;
break;
}
}
return res;
}
void print(int n, int sol[])
{
for(int i=0; i<=n*8; i++)
cout <<"-";
cout<< endl;
for (int r=0; r<n;r++)
{
for (int s=0; s<n; s++)
{
if (sol[s]!=r) cout << "|\t|";
else cout << "| o\t|";
}
cout << endl;
}
for(int i=0; i<=n*8; i++)
cout <<"-";
cout << "\n\n\n";
}
void Queens(int n, int r, int sol[])
{
for (int s=0; s<n; s++)
if ((sol[s]==-1) && diag(n,r,s,sol))
{
sol[s]=r;
if (r==n-1)
{
print(n,sol);
}
else
{
Queens(n,r+1,sol);
}
sol[s]=-1;
}
}
int main()
{
int n;
cout << "Unesite broj kraljica: ";
cin >> n;
int sol[n];
for (int i=0; i<n; i++)
sol[i]=-1;
Queens(n,0,sol);
system("pause");
return 0;
}