QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#239917#5509. Kooky Tic-Tac-Toeelkernos#AC ✓55ms3536kbC++234.8kb2023-11-05 00:30:022023-11-05 00:30:02

Judging History

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

  • [2023-11-05 00:30:02]
  • 评测
  • 测评结果:AC
  • 用时:55ms
  • 内存:3536kb
  • [2023-11-05 00:30:02]
  • 提交

answer

//Sylwia Sapkowska
#include <bits/stdc++.h>
#pragma GCC optimize("O3", "unroll-loops")
using namespace std;

void __print(int x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << "'" << x << "'";}
void __print(const char *x) {cerr << '"' << x << '"';}
void __print(const string &x) {cerr << '"' << x << '"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ", "; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? ", " : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifdef XOX
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

#define int long long
typedef pair<int, int> T;
const int oo = 1e18, oo2 = 1e9+7, K = 30;
const int mod = 998244353;

void solve(){
    int n, k; cin >> n >> k;
    vector a(n+2, vector<char>(n+2, '.'));
    vector<vector<T>>pos(3);
    auto ch = [&](char c){
        if (c == 'x') return 1;
        if (c == 'o') return 2;
        return 0;
    };
    auto rev = [&](int x){
        if (x == 1) return 'x';
        if (x == 2) return 'o';
        return '.';
    };
    vector<int>X = {1, 0, 1, -1};
    vector<int>Y = {0, 1, 1, 1};
    auto check = [&](vector<vector<char>>b){
        for (int i = 1; i<=n; i++){
            for (int j = 1; j<=n; j++){
                if (b[i][j] == '.') continue;
                for (int ck = 0; ck < 4; ck++){
                    T pos = {i, j};
                    bool ok = 1;
                    int tmp = k;
                    while (tmp--){
                        if (min(pos.first, pos.second) < 1 || max(pos.first, pos.second) > n){
                            ok = 0;
                            break;
                        }
                        ok &= (b[pos.first][pos.second] == b[i][j]);
                        pos = {pos.first+X[ck], pos.second + Y[ck]};
                    }
                    if (ok) return true;
                }
            }
        }
        return false;
    };
    vector<int>ile(3);
    for (int i = 1; i<=n; i++){
        for (int j = 1; j<=n; j++){
            cin >> a[i][j];
            ile[ch(a[i][j])]++;
            pos[ch(a[i][j])].emplace_back(i, j);
        }
    }
    if (abs(ile[1]-ile[2]) > 1){
        cout << "NIE\n";
        return;
    }
    vector<T>where;
    vector<int>curr(3);
    int tmp = 0;
    if (!check(a)){
        if (ile[1]+ile[2] == n*n) {
            //remis
            debug(2137);
            goto OK;
        } else {
            cout << "NIE\n";
            return;
        }
    }
    for (int i = 1; i<=n; i++){
        for (int j = 1; j<=n; j++){
            if (a[i][j] != '.'){
                char now = a[i][j];
                a[i][j] = '.';
                if (!check(a)){
                    where.emplace_back(i, j);
                    curr[ch(now)]++;
                } else {
                    tmp++;
                }
                a[i][j] = now;
            }
        }
    }
    
    debug(where, curr);
    debug(tmp, ile[1]+ile[2]);
    if ((int)where.empty() || (tmp == 0) || (curr[1] > 0 && curr[2] > 0)){
        cout << "NIE\n";
        return;
    }
    OK:;
    T last = {1, 1};
    if (where.empty()){
        for (int i = 1; i<=n; i++){
            for (int j =1; j<=n; j++){
                if (ile[ch(a[i][j])] == max(ile[1], ile[2])){
                    last = {i, j};
                    break;
                }
            }
        }
    } else last = where[0];
    int type = ch(a[last.first][last.second]);
    if (ile[type] < max(ile[1], ile[2])){
        cout << "NIE\n";
        return;
    }
    cout << "TAK\n";
    debug(last);
    vector vis(n+1, vector<bool>(n+1));
    vis[last.first][last.second] = 1;
    vector<T>ret = {last};
    debug(pos[1], pos[2]);
    while ((int)ret.size() < ile[1]+ile[2]){
        type = (type == 1 ? 2 : 1);
        auto now = pos[type].back();
        debug(now);
        if (vis[now.first][now.second]){
            pos[type].pop_back();
            now = pos[type].back();
        }
        vis[now.first][now.second] = 1;
        pos[type].pop_back();
        ret.emplace_back(now);
    } 
    reverse(ret.begin(), ret.end());
    for (auto [a, b]: ret) {
        cout << a << " " << b << "\n";
    }
}

int32_t main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int t = 1;
    cin >> t;
    while (t--) solve();

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3376kb

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 3
3 1
3 2
NIE
NIE
NIE
NIE

result:

ok correct (7 test cases)

Test #2:

score: 0
Accepted
time: 17ms
memory: 3536kb

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 3
3 1
3 2
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: 55ms
memory: 3440kb

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)