QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#78106#5509. Kooky Tic-Tac-ToexzyAC ✓74ms3460kbC++204.4kb2023-02-16 19:05:172023-02-16 19:05:19

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-02-16 19:05:19]
  • 评测
  • 测评结果:AC
  • 用时:74ms
  • 内存:3460kb
  • [2023-02-16 19:05:17]
  • 提交

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) {
        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)) {
            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: 100
Accepted
time: 0ms
memory: 3388kb

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 2
3 1
2 3
3 3
2 1
TAK
1 1
3 3
1 2
4 2
1 4
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:

ok correct (7 test cases)

Test #2:

score: 0
Accepted
time: 18ms
memory: 3460kb

input:

10000
3 3
x.o
xxx
o.o
3 3
xoo
oxx
xoo
3 2
xoo
oxx
xoo
3 3
xox
.o.
xox
3 2
xo.
..x
xo.
3 2
oox
.xo
o.x
5 5
xxx..
xxo.x
xoo..
xxxox
.oooo
3 3
xxx
.o.
oo.
3 2
x.o
xo.
..o
3 2
..x
xxo
.o.
3 3
xxo
o..
oxo
3 2
oox
..x
...
3 3
xxo
...
.ox
3 3
.xo
...
oox
3 3
.x.
xo.
o.o
3 2
o..
xxo
.ox
3 2
x.x
xoo
x.o
3 2
...

output:

TAK
1 1
1 3
2 2
3 1
2 3
3 3
2 1
TAK
1 2
1 1
1 3
2 2
2 1
2 3
3 2
3 1
3 3
NIE
NIE
NIE
NIE
NIE
TAK
2 2
1 2
3 1
1 3
3 2
1 1
NIE
NIE
NIE
NIE
NIE
NIE
NIE
NIE
NIE
NIE
NIE
NIE
NIE
NIE
NIE
NIE
TAK
1 2
1 1
1 3
1 4
2 3
2 2
2 4
4 2
3 1
4 3
3 2
4 1
NIE
NIE
NIE
NIE
NIE
TAK
2 1
1 1
2 3
1 3
2 4
1 4
3 1
2 2
3 3
3 2
...

result:

ok correct (10000 test cases)

Test #3:

score: 0
Accepted
time: 74ms
memory: 3340kb

input:

10000
6 4
x.xx.o
xo.o.x
ooox.o
o..xo.
..xxxo
o.oxx.
6 5
oooxxx
oxoxxo
xoooxo
xoxxxx
xooxox
xoxxxx
6 3
o.x.x.
oo.o.x
xx.oo.
.x.xx.
ooxo..
.xxo..
6 6
xoo..o
o.xx.x
oooooo
xx.x..
o..xx.
...xxx
6 5
xooxoo
ooxxoo
xxooxx
oxooxx
oxoxxx
xxoxoo
6 5
xoxxxo
ooooxo
ooxoxx
oxxoox
xxxxox
ooooxo
6 5
o....o
.ox.oo
...

output:

TAK
1 6
1 1
2 2
1 3
2 4
1 4
3 1
2 1
3 2
2 6
3 3
4 4
3 6
5 3
4 1
5 4
4 5
5 5
5 6
6 4
6 1
6 5
6 3
3 4
NIE
TAK
1 3
1 1
1 5
2 1
2 6
2 2
3 1
2 4
3 2
3 4
4 2
3 5
4 4
5 1
4 5
5 2
6 2
5 4
6 3
6 4
5 3
NIE
TAK
1 2
1 1
1 3
1 4
1 5
2 3
1 6
2 4
2 1
3 1
2 2
3 2
2 5
3 5
2 6
3 6
3 3
4 2
3 4
4 5
4 1
4 6
4 3
5 2
4 4
...

result:

ok correct (10000 test cases)