QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#99563#4234. Tic Tac Toe CountingAnwar_Gehad_Maghraby#WA 47ms3924kbC++201.9kb2023-04-23 02:48:142023-04-23 02:48:19

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-04-23 02:48:19]
  • Judged
  • Verdict: WA
  • Time: 47ms
  • Memory: 3924kb
  • [2023-04-23 02:48:14]
  • Submitted

answer

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

map<string, pair<int,int>> dp;
bool win(string &s, char c){
         string three= "";
         for(int i = 0; i < 9; i += 3){
                  if(s.substr(i, 3) == string(3, c))
                           return 1;
         }
         for(int i = 0; i < 3; i++){
                  bool good= 1;
                  for(int j = i; j < 9; j += 3){
                           good &= (s[j] == c);
                  }
                  if(good) return 1;
         }
         if(s[2] == s[4] && s[4] == s[6] && s[6] == c)
                  return 1;

         if(s[0] == s[4] && s[4] == s[8] && s[8] == c)
                  return 1;

         return 0;
}
pair<int,int> solve(string cur, int turn){
         if(dp.find(cur) != dp.end())
                  return dp[cur];

         auto &ret= dp[cur];
         ret= {0, 0};
         if(win(cur, 'X')) return ret= {1, 0};
         if(win(cur, 'O')) return ret= {0, 1};

         for(int i = 0; i < 9; i++){
                  if(cur[i] == '.'){
                           cur[i]= (turn ? 'X' : 'O');
                           auto nxt= solve(cur, turn ^ 1);
                           cur[i]= '.';
                           ret.first += nxt.first;
                           ret.second += nxt.second;
                  }
         }
         return ret;
}
void solve()
{
         string s; cin>>s;
         int xs= count(s.begin(), s.end(), 'X');
         int os= count(s.begin(), s.end(), 'O');

         if((win(s, 'X') && win(s, 'O')) || abs(xs - os) > 1 || xs < os){
                  cout<<"-1 -1\n";
                  return;
         }

         int turn= 1;
         if(xs > os) turn = 0;
         auto ans= solve(s, turn);
         cout<<ans.first<<" "<<ans.second<<"\n";
}
int32_t  main() {
         cin.tie(0);
         cin.sync_with_stdio(0);

         int T = 1; cin>>T;
         while(T--) solve();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 3380kb

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: 47ms
memory: 3824kb

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: 35ms
memory: 3924kb

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'