-
Notifications
You must be signed in to change notification settings - Fork 13
/
queen-control.cpp
50 lines (41 loc) · 1.38 KB
/
queen-control.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
#include <bits/stdc++.h>
#define QUEEN 'Q'
#define WHITE 'w'
#define BLACK 'b'
#define EMPTY '.'
struct Cell {
int x, y;
Cell(int x = 0, int y = 0) noexcept : x{x}, y{y} {}
operator bool() noexcept { return (0 <= x) && (8 > x) && (0 <= y) && (8 > y); }
friend Cell operator+(const Cell& lhs, const Cell& rhs) noexcept {
return { lhs.x + rhs.x, lhs.y + rhs.y };
}
};
using Grid = std::array<std::string, 8>;
using Move = Cell;
int main() {
std::deque<Move> moves{ {-1, -1}, {1, 1}, {-1, 1}, {1, -1},
{-1, 0}, {1, 0}, { 0, -1}, {0, 1} };
int res{ 0 };
Grid grid;
std::string c;
Cell queen;
getline(std::cin, c);
for ( int i{ 0 }; i < 8; ++i ) {
getline(std::cin, grid[i]);
if ( auto col{ grid[i].find_first_of(QUEEN) }; std::string::npos != col )
queen = { i, static_cast<int>(col) };
}
while ( !std::empty(moves) ) {
Move curMove{ moves.front() };
Cell curCell{ queen + curMove };
moves.pop_front();
while ( curCell ) {
char& v{ grid[curCell.x][curCell.y] };
if (!(((c[0] == WHITE) && (WHITE == v)) || ((c[0] == BLACK) && (BLACK == v)))) ++res;
if ( EMPTY != v ) break;
curCell = curCell + curMove;
}
}
std::cout << res << std::endl;
}