TOE2 - Tic-Tac-Toe ( II )

In the game of tic-tac-toe, two players take turns marking squares of an initially empty 3 × 3 grid with either X’s or O’s. The first player always marks squares using X’s, whereas the second player always marks squares using O’s. If at any point during the game either player manages to mark three consecutive squares in a row, column, or diagonal with his/her symbol, the game terminates.

Given a board configuration, your goal is to determine whether the board configuration represents the possible final state of a valid tic-tac-toe game.

Input

The input test file will contain multiple cases. Each test case consists of a single line containing 9 characters, which represent the 9 squares of a tic-tac-toe grid, given one row at a time. Each character on the line will either be ‘X’, ‘O’ (the letter O), or ‘.’ (indicating an unfilled square). The end-of-file is marked by a single line containing the word “end”.

Output

For each input test case, write a single line containing either the word “valid” or “invalid” indicating whether the given board configuration is the final state of some possible tic-tac-toe game.

Example

Input:
XXXOO.XXX
XOXOXOXOX
OXOXOXOXO
XXOOOXXOX
XO.OX...X
.XXX.XOOO
OOXXXOOXO
end

Output:
invalid
valid
invalid
valid
valid
invalid
invalid

Added by:Camilo Andrés Varela León
Date:2006-12-14
Time limit:0.206s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ERL JS-RHINO NODEJS PERL6 VB.NET
Resource:Stanford Local 2006

hide comments
2024-04-21 00:36:12
TEST CASE: XOXO.OXOX -> Invalid
it's a final possible end game -> there can not be any further moves to be taken, so there can not be any '.' left
2023-08-19 06:58:22
=)) i forgot it lol "the final state of some possible tic-tac-toe game"
2023-07-09 12:19:09
@ambuj2515 +1
2022-10-08 03:14:22
@sadat_11
for whom ask about the 2nd test case...
the last move was the cell in the center, that's how the X wins in 2 places
2022-06-26 15:18:49
we can use a similar solution as TOE1
but notice that there may be no winners
2022-01-11 19:54:57
Not every players are winner.
2022-01-11 19:54:20
Redirecting to : - https://www.spoj.com/problems/TOE1/
2021-11-29 20:50:38
Ngl This question really made me feel dumb and useless. :(
2021-06-14 17:07:34
I spent an hour trying to figure out wth is wrong- it was "imvalid" instead of invalid.
2021-01-13 14:37:40
finally i got ac after many wa (>﹏<).
I forget to handle that "end" at the end of test cases .
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.