-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathputQueens.cpp
More file actions
69 lines (66 loc) · 1.47 KB
/
Copy pathputQueens.cpp
File metadata and controls
69 lines (66 loc) · 1.47 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
//==================================
//name: Bui Ngoc Hai
//class: K51CD
//purpose: put Queen
//==================================
#include<iostream>
#include<conio.h>
using namespace std;
const int maxsize = 20; //20 is max size
int size,k,x[maxsize],a[maxsize],b[2*maxsize],c[2*maxsize]; // is is number of solutions, a, b, c are 3 arrays to mark columns and diagonal lines
void prepare()
{
cout << " input size = ";
cin >> size;
k=0;
for (int i=0;i<maxsize;i++)
{
x[i]=0;
a[i]=0;
}
for (int i=0;i<2*maxsize;i++)
{
b[i]=0;
c[i]=0;
}
}
void ouput()
{
k++;
cout << " solution" <<k<< endl<<endl;
for (int i=1;i<=size;i++)
{
for (int j=1;j<=x[i]-1;j++) cout << " * ";
cout << " H ";
for (int j=x[i]+1;j<=size;j++) cout << " * ";
cout << endl;
}
cout << endl;
}
void putQueen( int i)
{
for (int j=1;j<=size;j++)
if (a[j]==0 && b[i+j]==0 && c[i-j+maxsize]==0)
{
x[i]=j;
a[j]=1;
b[i+j]=1;
c[i-j+20]=1;
if (i==size) ouput();
else putQueen(i+1);
a[j]=0;
b[i+j]=0;
c[i-j+20]=0;
}
}
int main()
{
cout<<" program to put Queens in size*size chess board/n"<<endl;
prepare();
putQueen(1);
if (k==0) cout << " No solution" << endl;
else
cout << " there are " <<k<< " solutions " << endl;
getch();
return 0;
}