-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEight Numbers in a Cross.cpp
57 lines (44 loc) · 998 Bytes
/
Eight Numbers in a Cross.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
#include <iostream>
using namespace std;
bool ok(int q[], int c) {
int board[8][5] = { // helper Array
{-1},
{0, -1},
{0, 1, -1},
{0, 2, -1},
{1, 2, -1},
{1, 2, 3, 4, -1},
{2, 3, 5, -1},
{4, 5, 6, -1}
};
for(int i = 0; i < c; i++){
if(q[c] == q[i])
return false;
}
for(int i = 0; board[c][i]!=-1; i++){
if(q[board[c][i]]+1 == q[c] || q[board[c][i]]-1 == q[c])
return false;
}
return true;
}
void print(int q[]) {
static int solution = 1;
cout << "Solution #" << solution << ":" << endl;
cout << " " << q[1] <<" " << q[4] << endl;
cout << q[0] << " " << q[2] << " "<< q[5] << " " << q[7] << endl;
cout << " " << q[3] << " " << q[6] << endl;
cout << endl;
solution++;
}
void next(int q[], int c) {
if (c == 8)
print(q);
else for (q[c] = 1; q[c] < 9; ++q[c])
if (ok(q, c))
next(q, c+1);
}
int main() {
int q[8];
next(q, 0);
return 0;
}