TOE1 - Tic-Tac-Toe ( I )

Tic Tac Toe is a child's game played on a 3 by 3 grid. One player, X, starts by placing an X at an unoccupied grid position. Then the other player, O, places an O at an unoccupied grid position. Play alternates between X and O until the grid is filled or one player's symbols occupy an entire line (vertical, horizontal, or diagonal) in the grid.

We will denote the initial empty Tic Tac Toe grid with nine dots. Whenever X or O plays we fill in an X or an O in the appropriate position. The example below illustrates each grid configuration from the beginning to the end of a game in which X wins.

subir imagenes

Your job is to read a grid and to determine whether or not it could possibly be part of a valid Tic Tac Toe game. That is, is there a series of plays that can yield this grid somewhere between the start and end of the game?


The first line of input contains N, the number of test cases. 4N-1 lines follow, specifying N grid configurations separated by empty lines.


For each case print "yes" or "no" on a line by itself, indicating whether or not the configuration could be part of a Tic Tac Toe game.





hide comments
sinersnvrsleep: 2018-04-02 14:20:24

nice question we are over complicating it it has a simple solution

shahin mahmud: 2018-01-15 12:49:31

you should know
=> X player plays first
=>if X wins then O can't play anymore, same for when O wins
=>cases, X wins, O wins, Both wins, None wins

cake_is_a_lie: 2017-03-07 11:47:13

To the people complaining there are a lot of corner cases, I will say this: there is an elegant solution that is very simple to implement; if you have a lot of corner cases, you're over-complicating it.

Akash Garg: 2016-05-28 08:55:06

Instead of checking whether a configuration is valid, just play the game once and store all possible configurations in a map. O(1) time for checking if something is valid or not. And just few thousand possible configurations possible.

enigmus: 2016-02-25 13:15:29

I would advise people not to pursue this problem. It's easy to think of the solution but annoying to implement. There are so many possible exception one has to be aware about to get AC

minhthai: 2016-01-15 09:45:57

just be careful :)

dokany: 2015-07-09 17:58:48

Valid test case:


cracked: 2015-04-29 18:23:19

Some test cases:

Last edit: 2015-04-29 18:24:16
ankush sharma: 2015-04-14 15:44:48

X can win twice, be careful......

Shrikant: 2015-01-18 17:00:52

how this is valid ,places left only if
someone wins right?

Added by:Camilo Andrés Varela León
Time limit:0.161s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ERL JS-RHINO NODEJS PERL6 VB.NET
Resource:Waterloo local 2002.09.21