QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#554877#4234. Tic Tac Toe CountingFortitude#WA 20ms3972kbC++231.9kb2024-09-09 16:59:552024-09-09 16:59:55

Judging History

This is the latest submission verdict.

  • [2024-09-09 16:59:55]
  • Judged
  • Verdict: WA
  • Time: 20ms
  • Memory: 3972kb
  • [2024-09-09 16:59:55]
  • Submitted

answer

#include <bits/stdc++.h>
#define pb push_back
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define f first
#define s second
using namespace std;
using ll = long long;
using pii = pair <int, int>;
const int N = 2e5 + 5;

char s[3][3];
int cnt[2];
bool used[2][19683];
pair<int, int> dp[2][19683];

bool win(char c){
	for(int i = 0; i < 3; i++){
		if(s[i][0] == c && s[i][1] == c && s[i][2] == c) return 1;
		if(s[0][i] == c && s[1][i] == c && s[2][i] == c) return 1;
	}
	if(s[0][0] == c && s[1][1] == c && s[2][2] == c)
		return 1;
	if(s[0][2] == c && s[1][1] == c && s[2][0] == c)
		return 1;
	return 0;
}
pair<int, int> calc(bool type){
	int id = 0;
	for(int i = 0; i < 9; i++){
		if(s[i / 3][i % 3] == '.') id = id * 3;
		if(s[i / 3][i % 3] == 'X') id = id * 3 + 1;
		if(s[i / 3][i % 3] == 'O') id = id * 3 + 2;
	}
	
	if(win('X')){
		return {1, 0};
	}
	if(win('O')){
		return {0, 1};
	}
	if(used[type][id]){
		return dp[type][id];
	}
	used[type][id] = 1;
	
	pair<int, int> res = {0, 0};
	for(int i = 0; i < 3; i++){
		for(int j = 0; j < 3; j++){
			if(s[i][j] == '.'){
				if(!type){
					s[i][j] = 'X';
					auto x = calc(type ^ 1);
					res.f += x.f;
					res.s += x.s;
				}else{
					s[i][j] = 'O';
					auto x = calc(type ^ 1);
					res.f += x.f;
					res.s += x.s;
				}
				s[i][j] = '.';
			}
		}
	}
	
	return dp[type][id] = {res.f, res.s};
}
void solve(){
	int cntx = 0, cnty = 0;
	for(int i = 0; i < 9; i++){
		char c;
		cin >> c;
		s[i / 3][i % 3] = c;
		if(c == 'X') cntx++;
		if(c == 'O') cnty++;
	}
	if(cnty > cntx || abs(cntx - cnty) > 1){
		cout << "-1 -1\n";
		return;
	}
	if(win('X') && win('O')){
		cout << "-1 -1\n";
		return;
	}
	auto x = calc(cntx != cnty);
	cout << x.f << " " << x.s << "\n";
}

int main() {
	ios :: sync_with_stdio(false);
	cin.tie(nullptr);
	
	int t;
	cin >> t;
	while(t--){
		solve();
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 20ms
memory: 3896kb

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: 19ms
memory: 3972kb

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'