-
Notifications
You must be signed in to change notification settings - Fork 0
/
EightQueens.cpp
113 lines (75 loc) · 1.85 KB
/
EightQueens.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <string>
#include <iostream>
#include <vector>
using namespace std;
int BoardVisits[8][8] = {
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
};
bool populate(int x, int y) {
//vertical and horizontal
for (int lx = 0; lx < 8; lx++) {
if ((BoardVisits[lx][y] > 0 && lx != x) || (BoardVisits[x][lx] != 0 && lx != y))
return true;
}
//Diagonal 1:
int dx = x, dy = y;
while (dx > -1 && dy > -1) {
if (BoardVisits[dx][dy] > 0 && dx != x) {
//std::cout << dx << ' ' << dy << '\n';
return true;
}
dx--;
dy--;
}
dx = x, dy = y;
while (dx > -1 && dy < 8) {
if (BoardVisits[dx][dy] > 0 && dx != x) {
// std::cout << dx << ' ' << dy << '\n';
return true;
}
dx--;
dy++;
}
return false;
}
using namespace std;
struct vec2 {
int x, y;
};
int main() {
vector<vec2> Queens;
for (int y = 0; y < 8; y++) {
string l;
cin >> l;
for (int x = 0; x < 8; x++) {
if (l[x] == '*') {
Queens.push_back(vec2{ x,y });
BoardVisits[x][y]++;
}
}
}
for (int y = 0; y < 8; y++) {
for (int x = 0; x < 8; x++) {
// std::cout << BoardVisits[x][y];
}
//std::cout << '\n';
}
if(Queens.size() != 8) {
std::cout << "invalid\n";
return 0;
}
for(auto queen : Queens)
if (populate(queen.x, queen.y)) {
//std::cout << queen.x <<' ' << queen.y << '\n';
std::cout << "invalid\n";
return 0;
}
std::cout << "valid\n";
}