QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#78104 | #5509. Kooky Tic-Tac-Toe | xzy | WA | 0ms | 3392kb | C++20 | 4.4kb | 2023-02-16 19:02:18 | 2023-02-16 19:02:20 |
Judging History
answer
#include <bits/stdc++.h>
using ll = long long;
using namespace std;
void solve() {
int n, k; cin >> n >> k;
vector<string> grid(n);
for (auto& x : grid) cin >> x;
auto check_down = [&](int i, int j, char player) {
for (int row = i; row < i + k; ++row) {
if (row >= n || grid[row][j] != player) return false;
}
return true;
};
auto check_right = [&](int i, int j, char player) {
for (int col = j; col < j + k; ++col) {
if (col >= n || grid[i][col] != player) return false;
}
return true;
};
auto check_diag_down = [&](int i, int j, char player) {
for (int diag = 0; diag < k; ++diag) {
if (i + diag >= n || j + diag >= n || grid[i + diag][j + diag] != player) return false;
}
return true;
};
auto check_diag_up= [&](int i, int j, char player) {
for (int diag = 0; diag < k; ++diag) {
if (i - diag < 0 || j + diag >= n || grid[i - diag][j + diag] != player) return false;
}
return true;
};
array<vector<pair<int, int>>, 2> pos{};
for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) {
if (grid[i][j] == 'x') pos[0].emplace_back(i, j);
if (grid[i][j] == 'o') pos[1].emplace_back(i, j);
}
if (abs(int(pos[1].size()) - int(pos[0].size())) > 1) {
cout << "NIE\n";
return;
}
auto print_turns = [&](int first) {
cout << "TAK\n";
int y = 0;
for (int x = 0; x < pos[first].size(); ++x) {
cout << pos[first][x].first + 1 << ' ' << pos[first][x].second + 1<< '\n';
if (y < pos[1 - first].size()) {
cout << pos[1 - first][y].first + 1 << ' ' << pos[1 - first][y].second + 1<< '\n';
y++;
}
}
if (y < pos[1 - first].size()) {
cout << pos[1 - first][y].first + 1 << ' ' << pos[1 - first][y].second + 1<< '\n';
y++;
}
assert(y == pos[1 - first].size());
};
auto try_last_move = [&](int last_i, int last_j, array<vector<pair<int,int>>, 2> pos) {
int last_move = (grid[last_i][last_j] == 'x'? 0 : 1);
bool has_winning = false;
for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) if (grid[i][j] != '.') {
has_winning |= check_down(i, j, grid[i][j]);
has_winning |= check_right(i, j, grid[i][j]);
has_winning |= check_diag_up(i, j, grid[i][j]);
has_winning |= check_diag_down(i, j, grid[i][j]);
}
if (!has_winning) return false;
has_winning = false;
grid[last_i][last_j] = '.';
for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) if (grid[i][j] != '.') {
has_winning |= check_down(i, j, grid[i][j]);
has_winning |= check_right(i, j, grid[i][j]);
has_winning |= check_diag_up(i, j, grid[i][j]);
has_winning |= check_diag_down(i, j, grid[i][j]);
}
grid[last_i][last_j] = (last_move? 'o' : 'x');
if (has_winning) return false;
int first_turn;
{
if (pos[last_move].size() > pos[1 - last_move].size()) first_turn = last_move;
else if (pos[last_move].size() == pos[1 - last_move].size()) first_turn = 1 - last_move;
else {
return false;
}
}
pos[last_move].erase(std::remove(pos[last_move].begin(), pos[last_move].end(), pair<int,int>{last_i, last_j}));
print_turns(first_turn);
cout << last_i + 1 << ' ' << last_j + 1<< '\n';
return true;
};
for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) if (grid[i][j] != '.') {
if (try_last_move(i, j, pos)) {
return;
}
}
if (pos[0].size() + pos[1].size() != n * n) {
cout << "NIE\n";
return;
}
bool has_winning = false;
for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) if (grid[i][j] != '.') {
has_winning |= check_down(i, j, grid[i][j]);
has_winning |= check_right(i, j, grid[i][j]);
has_winning |= check_diag_up(i, j, grid[i][j]);
has_winning |= check_diag_down(i, j, grid[i][j]);
}
if (!has_winning) {
print_turns((pos[0].size() > pos[1].size()? 0 : 1));
return;
}
cout << "NIE\n";
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int t = 1; cin >> t;
while (t--) solve();
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 3392kb
input:
7 3 3 x.o xxx o.o 4 3 xx.x ...o ..o. .o.. 3 3 xoo oxx xoo 3 2 xoo oxx xoo 3 3 xox .o. xox 3 2 xo. ..x xo. 3 3 x.. .x. ..x
output:
TAK 1 1 1 3 2 1 3 1 2 2 3 3 2 3 2 1 TAK 1 1 2 4 1 2 3 3 1 4 4 2 2 4 TAK 1 2 1 1 1 3 2 2 2 1 2 3 3 2 3 1 3 3 NIE NIE NIE NIE
result:
wrong answer String has incorrect length (test case 2)