QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#105449#4234. Tic Tac Toe Countingmarsxiang5902WA 9ms4340kbC++171.7kb2023-05-14 04:40:052023-05-14 04:40:06

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-14 04:40:06]
  • Judged
  • Verdict: WA
  • Time: 9ms
  • Memory: 4340kb
  • [2023-05-14 04:40:05]
  • Submitted

answer

#include <bits/stdc++.h>
using namespace std;

const int MN = 9, MD = 1<<MN*2;

int T, dp[MD][2];

inline int bitAt(int b, int i) { return (b>>i*2)&3; }
inline bool invalid(int b) {
    int nx = 0, no = 0;
    for (int i = 0; i < 9; i++) {
        if (bitAt(b, i) == 3) return 1;
        nx += bitAt(b, i) == 1;
        no += bitAt(b, i) == 2;
    }
    return !(nx == no || nx == no+1);
}
inline int isWin(int b) {
    int ans = 0;
    for (int z: {1,2}) {
        bool b3 = 1, b4 = 1;
        for (int i = 0; i < 3; i++) {
            bool b1 = 1, b2 = 1;
            for (int j = 0; j < 3; j++) {
                b1 &= bitAt(b, i*3+j) == z;
                b2 &= bitAt(b, j*3+i) == z;
            }
            if (b1 || b2) ans |= z;
            b3 &= bitAt(b, i*3+i) == z;
            b4 &= bitAt(b, i*3+2-i) == z;
        }
        if (b3 || b4) ans |= z;;
    } return ans;
}

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

    for (int b = MD; b--; ) {
        if (invalid(b)) continue;
        if (int w = isWin(b); w) {
            if (w < 3) dp[b][w-1] = 1;
            continue;
        }
        for (int i = 0; i < 9; i++) {
            int turn = __builtin_popcount(b) & 1 ? 2 : 1;
            if (bitAt(b, i) == 0) {
                dp[b][0] += dp[b|turn<<i*2][0];
                dp[b][1] += dp[b|turn<<i*2][1];
            }
        }
    }
    cin >> T;
    for (int t = 0; t < T; t++) {
        int b = 0; string s; cin >> s;
        for (int i = 0; i < 9; i++)
            b |= (s[i] == 'O' ? 2 : s[i] == 'X') << i*2;
        if (!dp[b][0] && !dp[b][1]) printf("-1 -1\n");
        else printf("%d %d\n", dp[b][0], dp[b][1]);
    }


    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 4ms
memory: 4340kb

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: 8ms
memory: 4272kb

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: 9ms
memory: 4188kb

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: '1 0'