QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#83757#5509. Kooky Tic-Tac-Toeskittles1412AC ✓112ms3440kbC++175.4kb2023-03-03 13:31:332023-03-03 13:32:01

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-03-03 13:32:01]
  • 评测
  • 测评结果:AC
  • 用时:112ms
  • 内存:3440kb
  • [2023-03-03 13:31:33]
  • 提交

answer

#include "bits/extc++.h"

using namespace std;

template <typename T>
void dbgh(const T& t) {
    cerr << t << endl;
}

template <typename T, typename... U>
void dbgh(const T& t, const U&... u) {
    cerr << t << " | ";
    dbgh(u...);
}

#ifdef DEBUG
#define dbg(...)                                           \
    cerr << "L" << __LINE__ << " [" << #__VA_ARGS__ << "]" \
         << ": ";                                          \
    dbgh(__VA_ARGS__)
#else
#define cerr   \
    if (false) \
    cerr
#define dbg(...)
#endif

#define endl "\n"
#define long int64_t
#define sz(x) int(std::size(x))

template <typename T>
T reversed(T t) {
    reverse(begin(t), end(t));
    return t;
}

template <typename T>
vector<vector<T>> transposed(const vector<vector<T>>& arr) {
    int n = sz(arr), m = sz(arr[0]);
    vector<vector<T>> ans(m, vector<T>(n));
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            ans[i][j] = arr[j][i];
        }
    }
    return ans;
}

bool c_win(const vector<vector<char>>& arr, int kv, char c) {
    auto c_row = [&](const vector<vector<char>>& arr) -> bool {
        int n = sz(arr), m = sz(arr[0]);
        for (int i = 0; i < n; i++) {
            for (int j = 0; j + kv <= m; j++) {
                for (int k = 0; k < kv; k++) {
                    if (arr[i][j + k] != c) {
                        goto loop;
                    }
                }
                return true;
            loop:;
            }
        }
        return false;
    };
    auto c_diag = [&](const vector<vector<char>>& arr) -> bool {
        int n = sz(arr), m = sz(arr[0]);
        for (int i = 0; i + kv <= n; i++) {
            for (int j = 0; j + kv <= m; j++) {
                for (int k = 0; k < kv; k++) {
                    if (arr[i + k][j + k] != c) {
                        goto loop;
                    }
                }
                return true;
            loop:;
            }
        }
        return false;
    };
    return c_row(arr) || c_row(transposed(arr)) || c_diag(arr) ||
           c_diag(reversed(arr));
}

void grade(const vector<vector<char>>& arr, int kv, const vector<pair<int, int>>& ans) {
    int n = sz(arr), ord = -1;
    vector<vector<char>> tmp(n, vector<char>(n, '.'));
    for (int i = 0; i < sz(ans); i++) {
        auto& [x, y] = ans[i];
        assert(arr[x][y] == 'x' || arr[x][y] == 'o');
        int cv = (i + (arr[x][y] == 'x')) % 2;
        if (ord == -1) {
            ord = cv;
        }
        assert(ord == cv);
        tmp[x][y] = arr[x][y];
        if (i + 1 < sz(ans)) {
            assert(!c_win(tmp, kv, 'x') && !c_win(tmp, kv, 'o'));
        }
    }
    assert(arr == tmp);
}

void solve() {
    int n, kv;
    cin >> n >> kv;
    vector<vector<char>> arr(n);
    for (auto& a : arr) {
        string s;
        cin >> s;
        a = vector<char>(begin(s), end(s));
    }
    auto tarr = arr;
    vector<pair<int, int>> xs, os;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (arr[i][j] == 'x') {
                xs.emplace_back(i, j);
            } else if (arr[i][j] == 'o') {
                os.emplace_back(i, j);
            }
        }
    }
    auto print_xo = [&]() -> void {
        assert(sz(xs) >= sz(os));
        vector<pair<int, int>> ans;
        for (int k = 0; k < sz(xs) + sz(os); k++) {
            if (k % 2 == 0) {
                ans.push_back(xs[k / 2]);
            } else {
                ans.push_back(os[k / 2]);
            }
        }
        grade(tarr, kv, ans);
        cout << "TAK" << endl;
        for (auto& [x, y] : ans) {
            cout << x + 1 << " " << y + 1 << endl;
        }
    };
    bool bx = c_win(arr, kv, 'x'), bo = c_win(arr, kv, 'o');
    if (bx + bo != 1) {
        if (!bx && !bo && abs(sz(os) - sz(xs)) <= 1) {
            for (auto& a : arr) {
                for (auto& b : a) {
                    if (b == '.') {
                        goto loop;
                    }
                }
            }
            if (sz(os) > sz(xs)) {
                swap(xs, os);
            }
            print_xo();
            return;
        loop:;
        }
        cout << "NIE" << endl;
        return;
    }
    if (bo) {
        for (auto& a : arr) {
            for (auto& b : a) {
                if (b == 'x') {
                    b = 'o';
                } else if (b == 'o') {
                    b = 'x';
                }
            }
        }
        swap(xs, os);
    }
    if (!(sz(xs) == sz(os) || sz(xs) == sz(os) + 1)) {
        cout << "NIE" << endl;
        return;
    }
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (arr[i][j] == 'x') {
                auto tmp = arr;
                tmp[i][j] = '.';
                if (c_win(tmp, kv, 'x')) {
                    continue;
                }
                xs.erase(find(begin(xs), end(xs), pair<int, int> {i, j}));
                xs.emplace_back(i, j);
                if (sz(xs) == sz(os)) {
                    swap(xs, os);
                }
                print_xo();
                return;
            }
        }
    }
    cout << "NIE" << endl;
}

int main() {
    cin.tie(nullptr);
    cin.exceptions(ios::failbit);
    ios_base::sync_with_stdio(false);
    int tcs;
    cin >> tcs;
    while (tcs--) {
        solve();
    }
}

详细

Test #1:

score: 100
Accepted
time: 2ms
memory: 3396kb

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: 28ms
memory: 3440kb

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: 112ms
memory: 3416kb

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 1
1 2
1 4
1 3
2 3
1 5
2 4
1 6
3 1
2 1
3 2
2 2
3 5
2 5
3 6
2 6
4 2
3 3
4 5
3 4
4 6
4 1
5 2
4 3
5 4
...

result:

ok correct (10000 test cases)