// ====================================
// author: M_sea
// website: https://n-sea-blog.com/
// ====================================
#include <bits/stdc++.h>
#define file(x) freopen(#x".in", "r", stdin), freopen(#x".out", "w", stdout)
#define debug(...) fprintf(stderr, __VA_ARGS__)
using namespace std;
typedef long long ll;
int read() {
int X = 0, w = 1; char c = getchar();
while (c < '0' || c > '9') { if (c == '-') w = -1; c = getchar(); }
while (c >= '0' && c <= '9') X = X * 10 + c - '0', c = getchar();
return X * w;
}
const int N = 20;
int n = 19;
char a[N][N];
ll pw[N];
mt19937 rnd(time(0));
void PrintBoard() {
printf("\033c");
printf(" ");
for (int i = 1; i <= n; ++i) printf("%3d", i);
puts("");
for (int i = 1; i <= n; ++i) {
printf("%2d", i);
for (int j = 1; j <= n; ++j) printf(" %c", a[i][j]);
putchar('\n');
}
}
void CheckWin(char c) {
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j) {
if (i + 4 <= n && a[i][j] == c && a[i + 1][j] == c && a[i + 2][j] == c && a[i + 3][j] == c && a[i + 4][j] == c) {
PrintBoard(), printf("%c Wins", c);
exit(0);
}
if (j + 4 <= n && a[i][j] == c && a[i][j + 1] == c && a[i][j + 2] == c && a[i][j + 3] == c && a[i][j + 4] == c) {
PrintBoard(), printf("%c Wins", c);
exit(0);
}
if (i + 4 <= n && j + 4 <= n && a[i][j] == c && a[i + 1][j + 1] == c && a[i + 2][j + 2] == c && a[i + 3][j + 3] == c && a[i + 4][j + 4] == c) {
PrintBoard(), printf("%c Wins", c);
exit(0);
}
if (i + 4 <= n && j + 4 <= n && a[i][j + 4] == c && a[i + 1][j + 3] == c && a[i + 2][j + 2] == c && a[i + 3][j + 1] == c && a[i + 4][j] == c) {
PrintBoard(), printf("%c Wins", c);
exit(0);
}
}
}
void PutWhite() {
puts("Input:");
int x = read(), y = read();
while (a[x][y] != '.') puts("Invalid, please input again:"), x = read(), y = read();
a[x][y] = 'W';
}
ll CalcScore(int x, int y) {
a[x][y] = 'B';
ll ans = 0;
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j) {
if (i + 4 <= n && a[i][j] != 'W' && a[i + 1][j] != 'W' && a[i + 2][j] != 'W' && a[i + 3][j] != 'W' && a[i + 4][j] != 'W') {
int k = (a[i][j] == 'B') + (a[i + 1][j] == 'B') + (a[i + 2][j] == 'B') + (a[i + 3][j] == 'B') + (a[i + 4][j] == 'B');
if (k) ans += pw[2 * k - 1];
}
if (j + 4 <= n && a[i][j] != 'W' && a[i][j + 1] != 'W' && a[i][j + 2] != 'W' && a[i][j + 3] != 'W' && a[i][j + 4] != 'W') {
int k = (a[i][j] == 'B') + (a[i][j + 1] == 'B') + (a[i][j + 2] == 'B') + (a[i][j + 3] == 'B') + (a[i][j + 4] == 'B');
if (k) ans += pw[2 * k - 1];
}
if (i + 4 <= n && j + 4 <= n && a[i][j] != 'W' && a[i + 1][j + 1] != 'W' && a[i + 2][j + 2] != 'W' && a[i + 3][j + 3] != 'W' && a[i + 4][j + 4] != 'W') {
int k = (a[i][j] == 'B') + (a[i + 1][j + 1] == 'B') + (a[i + 2][j + 2] == 'B') + (a[i + 3][j + 3] == 'B') + (a[i + 4][j + 4] == 'B');
if (k) ans += pw[2 * k - 1];
}
if (i + 4 <= n && j + 4 <= n && a[i][j + 4] != 'W' && a[i + 1][j + 3] != 'W' && a[i + 2][j + 2] != 'W' && a[i + 3][j + 1] != 'W' && a[i + 4][j] != 'W') {
int k = (a[i][j + 4] == 'B') + (a[i + 1][j + 3] == 'B') + (a[i + 2][j + 2] == 'B') + (a[i + 3][j + 1] == 'B') + (a[i + 4][j] == 'B');
if (k) ans += pw[2 * k - 1];
}
if (i + 4 <= n && a[i][j] != 'B' && a[i + 1][j] != 'B' && a[i + 2][j] != 'B' && a[i + 3][j] != 'B' && a[i + 4][j] != 'B') {
int k = (a[i][j] == 'W') + (a[i + 1][j] == 'W') + (a[i + 2][j] == 'W') + (a[i + 3][j] == 'W') + (a[i + 4][j] == 'W');
if (k) ans -= pw[2 * k];
}
if (j + 4 <= n && a[i][j] != 'B' && a[i][j + 1] != 'B' && a[i][j + 2] != 'B' && a[i][j + 3] != 'B' && a[i][j + 4] != 'B') {
int k = (a[i][j] == 'W') + (a[i][j + 1] == 'W') + (a[i][j + 2] == 'W') + (a[i][j + 3] == 'W') + (a[i][j + 4] == 'W');
if (k) ans -= pw[2 * k];
}
if (i + 4 <= n && j + 4 <= n && a[i][j] != 'B' && a[i + 1][j + 1] != 'B' && a[i + 2][j + 2] != 'B' && a[i + 3][j + 3] != 'B' && a[i + 4][j + 4] != 'B') {
int k = (a[i][j] == 'W') + (a[i + 1][j + 1] == 'W') + (a[i + 2][j + 2] == 'W') + (a[i + 3][j + 3] == 'W') + (a[i + 4][j + 4] == 'W');
if (k) ans -= pw[2 * k];
}
if (i + 4 <= n && j + 4 <= n && a[i][j + 4] != 'B' && a[i + 1][j + 3] != 'B' && a[i + 2][j + 2] != 'B' && a[i + 3][j + 1] != 'B' && a[i + 4][j] != 'B') {
int k = (a[i][j + 4] == 'W') + (a[i + 1][j + 3] == 'W') + (a[i + 2][j + 2] == 'W') + (a[i + 3][j + 1] == 'W') + (a[i + 4][j] == 'W');
if (k) ans -= pw[2 * k];
}
}
a[x][y] = '.';
return ans + rnd() % 250;
}
std::pair<int, int> FindPlace() {
int ansx, ansy; ll ans = -1e18;
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j) {
if (a[i][j] != '.') continue;
ll now = CalcScore(i, j);
if (now > ans) ansx = i, ansy = j, ans = now;
}
return std::make_pair(ansx, ansy);
}
void PutBlack() {
int x, y; std::tie(x, y) = FindPlace();
a[x][y] = 'B';
}
int main() {
pw[0] = 1;
for (int i = 1; i <= 10; ++i) pw[i] = pw[i - 1] * 50;
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j)
a[i][j] = '.';
a[10][10] = 'B';
while (1) {
PrintBoard();
PutWhite();
CheckWin('W');
PutBlack();
CheckWin('B');
}
return 0;
}