-
Notifications
You must be signed in to change notification settings - Fork 0
/
TOE1 - Tic-Tac-Toe ( I ).cpp
81 lines (59 loc) · 2.03 KB
/
TOE1 - Tic-Tac-Toe ( I ).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
// https://www.spoj.com/problems/TOE1/
#include <bits/stdc++.h>
using namespace std;
bool is_win(string config) {
bool rows =
(config[0] != '.' && config[0] == config[1] && config[1] == config[2]) ||
(config[3] != '.' && config[3] == config[4] && config[4] == config[5]) ||
(config[6] != '.' && config[6] == config[7] && config[7] == config[8]);
bool columns =
(config[0] != '.' && config[0] == config[3] && config[3] == config[6]) ||
(config[1] != '.' && config[1] == config[4] && config[4] == config[7]) ||
(config[2] != '.' && config[2] == config[5] && config[5] == config[8]);
bool diagonals =
(config[0] != '.' && config[0] == config[4] && config[4] == config[8]) ||
(config[2] != '.' && config[2] == config[4] && config[4] == config[6]);
return rows || columns || diagonals;
}
int main() {
int n;
cin>>n;
while(n--){
string board="";
string start=".........";
queue<pair<string, bool>> boards;
boards.push({start, 1});
string value="no";
for(int i=0;i<3;i++)
{
string p;
cin>>p;
board+=p;
}
while(boards.size()){
string cur_board=boards.front().first;
bool X=boards.front().second;
boards.pop();
if(cur_board==board)
{
value="yes";
break;
}
if(is_win(cur_board)) continue;
for(int i=0;i<9;i++)
{
if(cur_board[i]=='.')
{
cur_board[i]= X?'X':'O';
if(cur_board[i]==board[i])
{
boards.push({cur_board,!X});
}
cur_board[i]='.';
}
}
}
cout<<value<<endl;
}
return 0;
}