-
Notifications
You must be signed in to change notification settings - Fork 1
/
141.cc
58 lines (52 loc) · 1.41 KB
/
141.cc
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
#include <bitset>
#include <iostream>
#include <unordered_set>
enum { MAX_SIZE = 50, MAX_SIZE_SQUARE = MAX_SIZE * MAX_SIZE, };
typedef std::bitset<MAX_SIZE_SQUARE> Board;
int ComputeIndex(int x, int y, int size) { return (x - 1) * size + (y - 1); }
Board Rotate(const Board& board, int size) {
Board out;
for (int x = 1; x <= size; x++) {
for (int y = 1; y <= size; y++) {
out[ComputeIndex(y, size - x + 1, size)] =
board[ComputeIndex(x, y, size)];
}
}
return out;
}
void DeclareWinner(int move) {
int winner = move % 2 + 1;
std::cout << "Player " << winner << " wins on move " << move << std::endl;
}
int main() {
std::unordered_set<Board> boards;
Board board, rotated;
int size;
while (std::cin >> size && size != 0) {
boards.clear();
board.reset();
bool is_game_over = false;
for (int move = 1; move <= size * 2; move++) {
int x, y;
std::string action;
std::cin >> x >> y >> action;
if (!is_game_over) {
board.flip(ComputeIndex(x, y, size));
rotated = board;
for (int i = 0; i < 4; i++) {
if (boards.find(rotated) != boards.end()) {
DeclareWinner(move);
is_game_over = true;
break;
}
rotated = Rotate(rotated, size);
}
boards.insert(board);
}
}
if (!is_game_over) {
std::cout << "Draw" << std::endl;
}
}
return 0;
}