-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTOE2 - Tic-Tac-Toe ( II ).cpp
89 lines (63 loc) · 2.21 KB
/
TOE2 - Tic-Tac-Toe ( II ).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
84
85
86
87
88
89
// https://www.spoj.com/problems/TOE2/
#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;
}
bool is_last(string config)
{ if(is_win(config))
return true;
for(int i=0;i<config.length();i++)
if(config[i]=='.')
return false;
return true;
}
int main() {
while(true){
string board="";
cin>>board;
if(board=="end")
break;
string start=".........";
queue<pair<string, bool>> boards;
boards.push({start, 1});
string value="invalid";
while(boards.size()){
string cur_board=boards.front().first;
bool X=boards.front().second;
boards.pop();
if(cur_board==board)
{
if(is_last(cur_board))
value="valid";
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;
}