-
Notifications
You must be signed in to change notification settings - Fork 0
/
Assignment3_2DnQueensGoTo.cpp
63 lines (51 loc) · 896 Bytes
/
Assignment3_2DnQueensGoTo.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
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int q[8][8] = { 0 };
q[0][0] = 1;
int c = 0, r = 0, n = 0;
col:
c++;
if (c == 8) {
n++;
goto print;
}
r = -1;
row:
r++;
if (r == 8) goto backtrack;
//horizontal test
for (int i = 0;i < c;i++) {
if (q[r][i] == 1) goto row;
}
//up-diagnal test
for (int i = 1; (r - i) >= 0 && (c-i) >= 0; i++) {
if (q[r - i][c - i] == 1) goto row;
}
//down-diagnal test
for (int i = 1; (r + i) < 8 && (c - i) >= 0;i++) {
if (q[r + i][c - i] == 1) goto row;
}
//place queen
q[r][c] = 1;
goto col;
backtrack:
c--;
if (c == -1) return 0;
r = 0;
while (q[r][c] != 1) r++;
q[r][c] = 0;
goto row;
print:
cout << "Solution number: "<< n << endl;
for (int r = 0;r < 8;r++) {
for (int c = 0;c < 8;c++) {
cout << q[r][c] << " ";
}
cout << endl;
}
cout << endl;
goto row;
return 0;
}