QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#125420#5509. Kooky Tic-Tac-ToeKKT89AC ✓17ms3512kbC++175.2kb2023-07-16 17:54:472023-07-16 17:54:48

Judging History

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

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

answer

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) {
    return (ull)rng() % B;
}

int n,k;
char board[8][8];
int s[8][8];
vector<pair<int,int>> v[3];

int tate[3][8][8];
int yoko[3][8][8];
int rd[3][8][8];
int ld[3][8][8];

int tr(char c){
    if(c == '.')return 0;
    else if(c == 'o')return 1;
    else return 2;
}

void NO(){
    cout << "NIE" << "\n";
}

void solve(){
    int sz1 = v[1].size();
    int sz2 = v[2].size();
    int bit = 0;
    if(sz1-1 == sz2) bit = 1;
    else if(sz2-1 == sz1) bit = 2;
    else if(sz1 == sz2) bit = 3;
    else{
        NO();
        return;
    }
    bool f = false;
    int tc = 0;
    int yc = 0;
    int rdc = 0;
    int ldc = 0;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            if(j+k <= n){
                for (int l = 0; l < k; ++l) {
                    yoko[s[i][j+l]][i][j]++;
                }
            }
            if(i+k <= n){
                for (int l = 0; l < k; ++l) {
                    tate[s[i+l][j]][i][j]++;
                }
            }
            if(i+k <= n and j+k <= n){
                for (int l = 0; l < k; ++l) {
                    rd[s[i+l][j+l]][i][j]++;
                }
            }
            if(i+k <= n and k-1 <= j){
                for (int l = 0; l < k; ++l) {
                    ld[s[i+l][j-l]][i][j]++;
                }
            }
            for (int l = 1; l < 3; ++l) {
                if(tate[l][i][j] == k){
                    tc++;
                }
                if(yoko[l][i][j] == k){
                    yc++;
                }
                if(rd[l][i][j] == k){
                    rdc++;
                }
                if(ld[l][i][j] == k){
                    ldc++;
                }
            }
        }
    }
    if(tc+yc+rdc+ldc != 0) f = true;
    if(!f and v[0].size() != 0){
        NO();
        return;
    }
//    cerr << bit << endl;
//    cerr << tc << " " << yc << " " << rdc << " " << ldc << endl;
    vector<pair<int,int>> res;
    int last = -1;
    for (int r = 1; r < 3; ++r) {
        // rを取り除く
        if(bit&r){
            for (int p = 0; p < v[r].size(); ++p) {
                auto [x,y] = v[r][p];
                {
                    int cnt = 0;
                    for (int i = 0; i < k; ++i) {
                        int ny = y-i;
                        if(0 <= ny and yoko[r][x][ny] == k){
                            cnt++;
                        }
                    }
                    if(cnt != yc) continue;
                }
                {
                    int cnt = 0;
                    for (int i = 0; i < k; ++i) {
                        int nx = x-i;
                        if(0 <= nx and tate[r][nx][y] == k){
                            cnt++;
                        }
                    }
                    if(cnt != tc) continue;
                }
                {
                    int cnt = 0;
                    for (int i = 0; i < k; ++i) {
                        int nx = x-i;
                        int ny = y-i;
                        if(0 <= nx and 0 <= ny and rd[r][nx][ny] == k){
                            cnt++;
                        }
                    }
                    if(cnt != rdc) continue;
                }
                {
                    int cnt = 0;
                    for (int i = 0; i < k; ++i) {
                        int nx = x-i;
                        int ny = y+i;
                        if(0 <= nx and ny < n and ld[r][nx][ny] == k){
                            cnt++;
                        }
                    }
                    if(cnt != ldc) continue;
                }
                // ここまで来たらおっけー
                last = r;
                res.push_back({x,y});
                swap(v[r][p], v[r].back());
                v[r].pop_back();
                break;
            }
            if(last != -1) break;
        }
    }
    if(last == -1){
        NO();
        return;
    }
    else{
        while (v[3-last].size()){
            last = 3-last;
            res.push_back(v[last].back());
            v[last].pop_back();
        }
    }
    reverse(res.begin(), res.end());
    cout << "TAK" << "\n";
    for(auto &p:res){
        cout << p.first+1 << " " << p.second+1 << "\n";
    }
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int q; cin >> q;
    while (q--){
        cin >> n >> k;
        v[0].clear();
        v[1].clear();
        v[2].clear();
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < n; ++j) {
                cin >> board[i][j];
                s[i][j] = tr(board[i][j]);
                v[s[i][j]].push_back({i,j});
                for (int l = 0; l < 3; ++l) {
                    tate[l][i][j] = yoko[l][i][j] = rd[l][i][j] = ld[l][i][j] = 0;
                }
            }
        }
        solve();
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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

result:

ok correct (7 test cases)

Test #2:

score: 0
Accepted
time: 7ms
memory: 3468kb

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

result:

ok correct (10000 test cases)

Test #3:

score: 0
Accepted
time: 17ms
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
6 5
3 6
4 4
4 1
5 3
4 5
5 4
5 6
5 5
6 1
6 4
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 3
5 4
6 2
6 4
5 3
NIE
TAK
1 1
6 6
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)