-
Notifications
You must be signed in to change notification settings - Fork 1
/
QueensChessProblem194.cpp
71 lines (69 loc) · 1.25 KB
/
QueensChessProblem194.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
#include <iostream>
#include <iomanip>
using namespace std;
int mat[9][9] = { 0 };
int rows[9] = { 0 };
int num = 1;
int column = 0;
int sol_no = 1;
int check(int row, int col) {//check if row, col element coincides with any element
for (int i = 1; i < 9; i++) {
if (rows[i] != 0) {
if (rows[i] == row)
return 0;
else if (i == col)
return 0;
else if ((i + rows[i]) == (row + col))
return 0;
else if ((rows[i] - i) == (row - col))
return 0;
}
}
return 1;
}
void find(int i) {
int flag = 0;
if (i == 9) {
cout << setw(2) << sol_no++;
cout << " ";
for (int j = 1; j < 8; j++) {
cout << rows[j] << " ";
}
cout << rows[8]<<endl;
}
else {
for (int j = 1; j < 9; j++) {//for each row
if (rows[i] != 0) {
find(i + 1);
break;
}
int c = check(j, i);
if (c == 1) {
rows[i] = j;
find(i + 1);
rows[i] = 0;
}
}
}
}
int main() {
int n = 0;
cin >> n;
int ctr = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j < 9; j++) {
rows[j] = 0;
}
int row, col;
cin >> row >> col;
cout << "SOLN COLUMN" << endl;
cout << " # 1 2 3 4 5 6 7 8" << endl << endl;
column = col;
rows[col] = row;
sol_no = 1;
find(1);
if(i!=n)
cout << endl;
}
return 0;
}