-
Notifications
You must be signed in to change notification settings - Fork 0
/
slow_queens.cpp
48 lines (41 loc) · 909 Bytes
/
slow_queens.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
#include <iostream>
using namespace std;
int main() {
int b[8][8] = {0}, r, c = 0;
b[0][0] = 1;
next_col:
++c;
if (c == 8) { goto print; }
r = -1;
next_row:
++r;
if (r == 8) { goto backtrack; }
for (int i = 0; i < c; ++i) {
if (b[r][i] == 1) { goto next_row; }
}
for (int i = 1; (r - i) >= 0 && (c - i) >= 0; ++i) {
if (b[r - i][c - i] == 1) { goto next_row; }
}
for (int i = 1; (r + i) < 8 && (c - i) >= 0; ++i) {
if (b[r + i][c - i] == 1) { goto next_row; }
}
b[r][c] = 1;
goto next_col;
backtrack:
--c;
if (c == -1) { return 0; }
r = 0;
while (b[r][c] != 1) {
++r;
}
b[r][c] = 0;
goto next_row;
print:
for (int i = 0; i < 8; ++i) {
for (int j = 0; j < 8; ++j) {
cout << b[i][j];
}
cout << endl;
}
goto backtrack;
}