-
Notifications
You must be signed in to change notification settings - Fork 0
/
toe1.php
110 lines (91 loc) · 1.88 KB
/
toe1.php
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
$testMode = true;
if(!$testMode) {
$i = $testCount = 0;
$input = '';
while ($f = fgets(STDIN)) {
$input .= $f;
if ($i == 0)
$testCount = (int)$f;
$i++;
if ($i == $testCount * 4)
break;
}
} else
$input = "13
X.O
OO.
XXX
O.X
XX.
OOO
O.X
XXX
OOO
ooo
ooo
ooo
XXX
XXX
XXX
...
...
...
o..
...
...
ooo
xx.
xx.
ox.
...
...
xox
oxo
xox
oxo
xxx
oxo
xxx
oox
oox
xxx
xxx
ooo";
$inputLines = array_map(function($item) {
return trim($item);
}, explode("\n", $input));
$testCount = (int)$inputLines[0];
for($i = 0; $i < $testCount; $i++) {
$isPossible = false;
$counters = ['x' => 0, 'o' => 0, '.' => 0];
$field = [];
for($j = 0; $j < 3; $j++) {
$field[$j] = array_map('strtolower', str_split($inputLines[1 + $i * 4 + $j]));
for($k = 0; $k < 3; $k++)
$counters[$field[$j][$k]]++;
}
$isPossible = ($counters['x'] >= $counters['o'] && $counters['x'] - $counters['o'] < 2);
if($isPossible) {
$wins = ['x' => 0, 'o' => 0, '.' => 0];
for($j = 0; $j < 3; $j++) {
if($field[$j][0] == $field[$j][1] && $field[$j][0] == $field[$j][2])
$wins[$field[$j][0]]++;
if($field[0][$j] == $field[1][$j] && $field[0][$j] == $field[2][$j])
$wins[$field[0][$j]]++;
}
if($field[0][0] == $field[1][1] && $field[0][0] == $field[2][2])
$wins[$field[0][0]]++;
if($field[0][2] == $field[1][1] && $field[0][2] == $field[2][0])
$wins[$field[0][2]]++;
$isPossible = (
$wins['x'] <= 2 && $wins['o'] < 2
&& ($wins['x'] != $wins['o'] || $wins['o'] == 0)
);
if($isPossible && $wins['o'] && $counters['x'] > $counters['o'])
$isPossible = false;
}
echo $isPossible ? "yes" : "no";
if($i < $testCount - 1)
echo "\n";
}