QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#106257#4234. Tic Tac Toe Countingzaxwellmen#WA 25ms3572kbC++201.8kb2023-05-17 03:44:372023-05-17 03:44:40

Judging History

This is the latest submission verdict.

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-05-17 03:44:40]
  • Judged
  • Verdict: WA
  • Time: 25ms
  • Memory: 3572kb
  • [2023-05-17 03:44:37]
  • Submitted

answer

#include<bits/stdc++.h>

using namespace std;

typedef pair<int, int> pi;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define F0R(i, a) FOR(i, 0, a)
#define mp make_pair
#define F first 
#define S second

int n;
const int m = 19683;
string s;
pi dp[m];

bool win(vector<int> a, int turn) {
    bool row[3] = {true, true, true}, col[3] = {true, true, true}, d1 = true, d2 = true;
    F0R(i, 3) F0R(j, 3) if (a[3*i+j] != turn) {
        row[i] = false; col[j] = false;
        if (i==j) d1 = false;
        if (i==2-j) d2 = false;
    }
    return row[0] || row[1] || row[2] || col[0] || col[1] || col[2] || d1 || d2;
}
vector<int> to_vec(int x) {
    vector<int> a(9);
    F0R(i, 9) {
        a[i] = x % 3;
        x /= 3;
    }
    return a;
}
int from_vec(vector<int> a) {
    int pow = 1, x = 0;
    F0R(i, 9) {
        x += pow * a[i];
        pow *= 3;
    }
    return x;
}

void dfs(int x, int turn) {
    if (dp[x] != mp(0,0)) return;
    vector<int> a = to_vec(x);
    if (win(a,0)) {
        dp[x] = mp(1,0);
        return;
    }
    if (win(a,1)) {
        dp[x] = mp(0,1);
        return;
    }
    F0R(i, 9) {
        if (a[i] == 2) {
            a[i] = turn;
            int nx = from_vec(a);
            dfs(nx, 1-turn);
            dp[x].F += dp[nx].F;
            dp[x].S += dp[nx].S;
            a[i] = 2;
        }
    }
}

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    dfs(m-1, 0);
    cin >> n;
    F0R(test, n) {
        cin >> s;
        int x = 0, pow = 1;
        F0R(i, 9) {
            int a = 2;
            if (s[i]=='X') a = 0;
            if (s[i]=='O') a = 1;
            x += a*pow;
            pow *= 3;
        }
        if (dp[x] == mp(0,0)) cout << "-1 -1\n";
        else cout << dp[x].F << ' ' << dp[x].S << '\n';
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 25ms
memory: 3564kb

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: 17ms
memory: 3572kb

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 1211th lines differ - expected: '0 0', found: '-1 -1'