forked from Diusrex/UVA-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
10363 Tic Tac Toe.cpp
83 lines (63 loc) · 1.97 KB
/
10363 Tic Tac Toe.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <iostream>
#include <vector>
#include <string>
using namespace std;
// Check:
// if O == X, that X has not won
// if O + 1 == X, that O has not won
bool HasNotWon(const vector<string>& grid, char character)
{
if (grid[0][0] == character && grid[0][1] == character && grid[0][2] == character)
return false;
if (grid[1][0] == character && grid[1][1] == character && grid[1][2] == character)
return false;
if (grid[2][0] == character && grid[2][1] == character && grid[2][2] == character)
return false;
if (grid[0][0] == character && grid[1][0] == character && grid[2][0] == character)
return false;
if (grid[0][1] == character && grid[1][1] == character && grid[2][1] == character)
return false;
if (grid[0][2] == character && grid[1][2] == character && grid[2][2] == character)
return false;
if (grid[0][0] == character && grid[1][1] == character && grid[2][2] == character)
return false;
if (grid[0][2] == character && grid[1][1] == character && grid[2][0] == character)
return false;
return true;
}
int main()
{
int T;
cin >> T;
vector<string> grid(3);
while (T--)
{
int xCount = 0, oCount = 0;
for (int i = 0; i < 3; ++i)
{
cin >> grid[i];
for (int x = 0; x < 3; ++x)
{
if (grid[i][x] == 'X')
++xCount;
else if (grid[i][x] == 'O')
++oCount;
}
}
bool valid;
if (xCount == oCount)
{
valid = HasNotWon(grid, 'X');
}
else if (xCount == oCount + 1)
{
valid = HasNotWon(grid, 'O');
}
else
valid = false;
if (valid)
cout << "yes\n";
else
cout << "no\n";
}
}