QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#626278#4234. Tic Tac Toe Countingnickbelov#WA 157ms4008kbC++204.8kb2024-10-10 01:56:462024-10-10 01:56:46

Judging History

This is the latest submission verdict.

  • [2024-10-10 01:56:46]
  • Judged
  • Verdict: WA
  • Time: 157ms
  • Memory: 4008kb
  • [2024-10-10 01:56:46]
  • Submitted

answer

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>

using namespace std;
using namespace __gnu_pbds;

template<typename T>
ostream_iterator<T> oit(const string &s = " "){ return ostream_iterator<T>(cout,s.c_str()); }
inline auto rep(int l, int r) { return views::iota(min(l, r), r); }
inline auto rep(int n) { return rep(0, n); }
inline auto rep1(int l, int r) { return rep(l, r + 1); }
inline auto rep1(int n) { return rep(1, n + 1); }
inline auto per(int l, int r) { return rep(l, r) | views::reverse; }
inline auto per(int n) { return per(0, n); }
inline auto per1(int l, int r) { return per(l, r + 1); }
inline auto per1(int n) { return per(1, n + 1); }
#define A(a) begin(a),end(a)
inline auto len = ranges::ssize;

struct chash {
    static uint64_t splitmix64(uint64_t x) {
        // http://xorshift.di.unimi.it/splitmix64.c
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};
template<typename T, typename U> using pb_map = gp_hash_table<T, U, chash>;
template<typename T> using pb_set = gp_hash_table<T, null_type, chash>;
#define K first
#define V second

using ll = long long;
using ld = long double;

using vi = vector<int>;
using vii = vector<vector<int>>;
typedef vector<ll> vll;
using pll = pair<ll,ll>;
using pii = pair<int,int>;

using B = array<array<char,3>,3>;
string BS(B b){
    string s; for(auto &v : b) for(char c : v) s+=c;
    return s;
}
map<B,pii> dp;

void run()
{
    B g {};
    for(auto &v : g) for(auto &c : v) cin >> c;

    int xc=0,oc=0; //wrong number of guys
    {
        for(auto &v : g) for(auto c : v)
            xc+=c=='X',oc+=c=='O';
        
        if((xc-oc) != 0 and ((xc-oc)!=1))
            return void(cout << "-1 -1\n");
    }

    auto find_lines = [](char w, B &g){
        vector<set<pii>> lines;

        for(int i : rep(3)){ //horizontal
            set<pii> ln; bool good = true;
            for(int j : rep(3)){
                ln.insert({i,j});
                good and_eq g[i][j]==w;
            }
            if(good) lines.push_back(ln);
        }

        for(int j : rep(3)){ //vertical
            set<pii> ln; bool good = true;
            for(int i : rep(3)){
                ln.insert({i,j});
                good and_eq g[i][j]==w;
            }
            if(good) lines.push_back(ln);
        }

        { //diag1
            set<pii> ln; bool good = true;
            for(int i : rep(3))
                good and_eq g[i][i]==w,ln.insert({i,i});
            if(good) lines.push_back(ln);
        }
        { //diag2
            set<pii> ln; bool good = true;
            for(int i : rep(3))
                good and_eq g[i][3-i-1]==w,ln.insert({i,3-i-1});
            if(good) lines.push_back(ln);
        }
        return lines;
    };
    auto l1 = find_lines('X',g), l2 = find_lines('O',g);
    if(not l1.empty() and not l2.empty()){ //two winners?
        return void(cout << "-1 -1\n");
    }
    if(not l1.empty()){ //multiply non intersecting winning lines
        set<pii> l = l1[0];
        for(auto &st : l1){
            set<pii> nl;
            for(auto x : st) if(l.contains(x)) nl.insert(x);
            l=nl;
        }
        if(l.empty())
            return void(cout << "-1 -1\n");
    }
    if(not l2.empty()){
        set<pii> l = l2[0];
        for(auto &st : l2){
            set<pii> nl;
            for(auto x : st) if(l.contains(x)) nl.insert(x);
            l=nl;
        }
        if(l.empty())
            return void(cout << "-1 -1\n");
    }

    array<char,2> ca = {'X','O'};
    function<pii(B,int turn)> rec = [&](B state,int turn){
        if(dp.contains(state)) return dp[state];
        auto &here = dp[state]; here = {0,0};
        
        auto res = find_lines(ca[turn^1],state);
        if(not res.empty()){
            if(turn==0) here.second++; //if its player 1s turn now, player 2 must have just won
            else here.first++;
            return here;
        }

        for(int i : rep(3)){
            for(int j : rep(3)){
                if(state[i][j]!='.') continue;
                B nstate = state; nstate[i][j] = ca[turn];
                auto [add1,add2] = rec(nstate,turn^1);
                here.first+=add1,here.second+=add2;
            }
        }

        return here;
    };
    auto ans = rec(g,(xc+oc)%2);

    cout << ans.first << " " << ans.second << '\n';
}

int main()
{
    //KING OF THE WORLD...... U.W.T.B
    cin.tie(0);
    ios_base::sync_with_stdio(false);
    int t; cin>>t; while(t--) run();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

4
XX..O....
X...OX...
OOOX.X.X.
OOOXXX...

output:

191 194
232 200
0 1
-1 -1

result:

ok 4 lines

Test #2:

score: 0
Accepted
time: 157ms
memory: 4004kb

input:

100000
.........
.........
.........
.........
.........
.........
.........
.........
.........
.........
.........
.........
.........
.........
.........
.........
.........
.........
.........
.........
.........
.........
.........
.........
.........
.........
.........
.........
.........
......

output:

131184 77904
131184 77904
131184 77904
131184 77904
131184 77904
131184 77904
131184 77904
131184 77904
131184 77904
131184 77904
131184 77904
131184 77904
131184 77904
131184 77904
131184 77904
131184 77904
131184 77904
131184 77904
131184 77904
131184 77904
131184 77904
131184 77904
131184 77904
1...

result:

ok 100000 lines

Test #3:

score: -100
Wrong Answer
time: 68ms
memory: 4008kb

input:

100000
.........
X........
O........
.X.......
XX.......
OX.......
.O.......
XO.......
OO.......
..X......
X.X......
O.X......
.XX......
XXX......
OXX......
.OX......
XOX......
OOX......
..O......
X.O......
O.O......
.XO......
XXO......
OXO......
.OO......
XOO......
OOO......
...X.....
X..X.....
O.....

output:

131184 77904
14652 7896
-1 -1
14232 10176
-1 -1
1798 1276
-1 -1
2048 756
-1 -1
14652 7896
-1 -1
1832 1132
-1 -1
-1 -1
220 248
2048 756
268 144
-1 -1
-1 -1
1832 1132
-1 -1
1798 1276
220 248
-1 -1
-1 -1
-1 -1
-1 -1
14232 10176
-1 -1
1798 1276
-1 -1
-1 -1
264 188
1868 1080
239 126
-1 -1
-1 -1
-1 -1
312...

result:

wrong answer 882nd lines differ - expected: '-1 -1', found: '3 0'