-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathQuestion 3.cpp
More file actions
83 lines (81 loc) · 1.42 KB
/
Question 3.cpp
File metadata and controls
83 lines (81 loc) · 1.42 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
78
79
80
81
82
83
#include<iostream>
#include<vector>
#include<fstream>
#include<string>
#include<algorithm>
using namespace std;
int sol=0;
class queen
{
int n;
public:
void read()
{
cout<<"Enter board size\n";
cin>>n;
}
bool place(int x[10],int k)
{
for(int i=1;i<k;i++)
{
if(x[i]==x[k]||i+x[i]==k+x[k]||i-x[i]==k-x[k])
return false;
}
return true;
}
void nqueen()
{
int x[10];
int k=1;
x[k]=0;
while(k!=0)
{
x[k]++;
while((!place(x,k))&&(x[k]<=n))x[k]++;
if(x[k]<=n)
{
if(k==n)
{
sol++;
cout<<"Solution "<<sol<<":\n";
print(x);
cout<<endl;
}
else
{
k++;
x[k]=0;
}
}
else
k--;
}
}
void print(int x[10])
{
char c[10][10];
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
c[i][j]='0';
}
for(int i=1;i<=n;i++)
{
c[i][x[i]]='1';
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
cout<<c[i][j];
cout<<endl;
}
cout<<endl;
}
};
int main()
{
queen A;
A.read();
A.nqueen();
return 0;
}