-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTOE2 - Tic-Tac-Toe ( II ).cpp
More file actions
77 lines (73 loc) · 1.95 KB
/
Copy pathTOE2 - Tic-Tac-Toe ( II ).cpp
File metadata and controls
77 lines (73 loc) · 1.95 KB
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
#include<bits/stdc++.h>
using namespace std;
bool isWin(char arr[3][3], int i, int j, char ch){
bool resR = true, resC = true, resD = true;
for(int k = 0; k < 3; k++){
if(arr[i][k] != ch)
resR = false;
}
if(resR == true)
return true;
for(int k = 0; k < 3; k++){
if(arr[k][j] != ch)
resC = false;
}
if(resC == true)
return true;
if(i == 1 && j == 1){
for(int k = 0; k < 3; k++)
if(arr[k][k] != ch)
resD = false;
if(resD == true)
return true;
resD = true;
for(int k = 0; k < 3; k++)
if(arr[k][2 - k] != ch)
resD = false;
if(resD == true)
return true;
}
return false;
}
int main(){
int t;
cin >> t;
while(t--){
char arr[3][3];
int countX = 0, countO = 0;
bool winX = false, winO = false;
for(int i = 0; i < 3; i++)
for(int j = 0; j < 3; j++){
cin >> arr[i][j];
if(arr[i][j] == 'X')
countX++;
if(arr[i][j] == 'O')
countO++;
}
for(int i = 0; i < 3; i++)
for(int j = 0; j < 3; j++){
if(arr[i][j] == 'X' && isWin(arr, i, j, 'X'))
winX = true;
if(arr[i][j] == 'O' && isWin(arr, i, j, 'O'))
winO = true;
}
if(winX && winO){
cout << "Invalid" << endl;
continue;
}
if(winX && countX == countO + 1){
cout << "Valid" << endl;
continue;
}
if(winO && countX == countO){
cout << "Valid" << endl;
continue;
}
if((!winO && !winX) && (countX == countO || countX == countO + 1)){
cout << "Valid" << endl;
continue;
}
cout << "Invalid" << endl;
}
return 0;
}