-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path194.cpp
More file actions
65 lines (59 loc) · 1008 Bytes
/
Copy path194.cpp
File metadata and controls
65 lines (59 loc) · 1008 Bytes
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
#include<iostream>
#include<stdio.h>
using namespace std;
int row, col;
int plc[8];
int solNum;
bool valid(int r, int c) {
for (int i = 0; i < 8; i++) {
if (plc[i] != -1 && i != c && (r == plc[i] || c - r == i - plc[i] || c + r == i + plc[i])) {
return false;
}
}
return true;
}
void f(int dep) {
if (dep == 8) {
printf("%2d ", ++solNum);
for (int i = 0; i < 8; i++) {
cout << " " << plc[i]+1;
}
cout << endl;
}
else {
if (dep == col) {
f(dep + 1);
}
else {
for (int i = 0; i < 8; i++) {
if (valid(i, dep)) {
plc[dep] = i;
f(dep + 1);
plc[dep] = -1;
}
}
}
}
}
int main() {
int caseNum;
bool first = true;
cin >> caseNum;
for (int c = 0; c < caseNum; c++) {
cin >> row >> col;
row--; col--;
solNum = 0;
for (int i = 0; i < 8; i++) {
plc[i] = -1;
}
plc[col] = row;
if (!first) {
cout << endl;
}
first = false;
cout << "SOLN COLUMN\n";
cout << " # 1 2 3 4 5 6 7 8\n\n";
f(0);
}
return 0;
}